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 "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 {
57 } FrapsContext;
58 
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 
73 
74  return 0;
75 }
76 
77 /**
78  * Comparator - our nodes should ascend by count
79  * but with preserved symbol order
80  */
81 static 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  */
90 static 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;
95  GetBitContext gb;
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_free_vlc(&vlc);
127  return AVERROR_INVALIDDATA;
128  }
129  }
130  dst += stride;
131  }
132  ff_free_vlc(&vlc);
133  return 0;
134 }
135 
137  void *data, int *got_frame,
138  AVPacket *avpkt)
139 {
140  FrapsContext * const s = avctx->priv_data;
141  const uint8_t *buf = avpkt->data;
142  int buf_size = avpkt->size;
143  ThreadFrame frame = { .f = data };
144  AVFrame * const f = data;
145  uint32_t header;
146  unsigned int version,header_size;
147  unsigned int x, y;
148  const uint32_t *buf32;
149  uint32_t *luma1,*luma2,*cb,*cr;
150  uint32_t offs[4];
151  int i, j, ret, is_chroma;
152  const int planes = 3;
153  int is_pal;
154  uint8_t *out;
155 
156  if (buf_size < 4) {
157  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
158  return AVERROR_INVALIDDATA;
159  }
160 
161  header = AV_RL32(buf);
162  version = header & 0xff;
163  is_pal = buf[1] == 2 && version == 1;
164  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
165 
166  if (version > 5) {
167  avpriv_report_missing_feature(avctx, "Fraps version %u", version);
168  return AVERROR_PATCHWELCOME;
169  }
170 
171  buf += header_size;
172 
173  if (is_pal) {
174  unsigned needed_size = avctx->width * avctx->height + 1024;
175  needed_size += header_size;
176  if (buf_size != needed_size) {
177  av_log(avctx, AV_LOG_ERROR,
178  "Invalid frame length %d (should be %d)\n",
179  buf_size, needed_size);
180  return AVERROR_INVALIDDATA;
181  }
182  } else if (version < 2) {
183  unsigned needed_size = avctx->width * avctx->height * 3;
184  if (version == 0) needed_size /= 2;
185  needed_size += header_size;
186  /* bit 31 means same as previous pic */
187  if (header & (1U<<31)) {
188  *got_frame = 0;
189  return buf_size;
190  }
191  if (buf_size != needed_size) {
192  av_log(avctx, AV_LOG_ERROR,
193  "Invalid frame length %d (should be %d)\n",
194  buf_size, needed_size);
195  return AVERROR_INVALIDDATA;
196  }
197  } else {
198  /* skip frame */
199  if (buf_size == 8) {
200  *got_frame = 0;
201  return buf_size;
202  }
203  if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
204  av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
205  return AVERROR_INVALIDDATA;
206  }
207  for (i = 0; i < planes; i++) {
208  offs[i] = AV_RL32(buf + 4 + i * 4);
209  if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
210  av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
211  return AVERROR_INVALIDDATA;
212  }
213  }
214  offs[planes] = buf_size - header_size;
215  for (i = 0; i < planes; i++) {
216  av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
217  if (!s->tmpbuf)
218  return AVERROR(ENOMEM);
219  }
220  }
221 
223  f->key_frame = 1;
224 
225  avctx->pix_fmt = version & 1 ? is_pal ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
226  avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
228  avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
229 
230  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
231  return ret;
232 
233  switch (version) {
234  case 0:
235  default:
236  /* Fraps v0 is a reordered YUV420 */
237  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
238  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
239  avctx->width, avctx->height);
240  return AVERROR_INVALIDDATA;
241  }
242 
243  buf32 = (const uint32_t*)buf;
244  for (y = 0; y < avctx->height / 2; y++) {
245  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
246  luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
247  cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
248  cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
249  for (x = 0; x < avctx->width; x += 8) {
250  *luma1++ = *buf32++;
251  *luma1++ = *buf32++;
252  *luma2++ = *buf32++;
253  *luma2++ = *buf32++;
254  *cr++ = *buf32++;
255  *cb++ = *buf32++;
256  }
257  }
258  break;
259 
260  case 1:
261  if (is_pal) {
262  uint32_t *pal = (uint32_t *)f->data[1];
263 
264  for (y = 0; y < 256; y++) {
265  pal[y] = AV_RL32(buf) | 0xFF000000;
266  buf += 4;
267  }
268 
269  for (y = 0; y <avctx->height; y++)
270  memcpy(&f->data[0][y * f->linesize[0]],
271  &buf[y * avctx->width],
272  avctx->width);
273  } else {
274  /* Fraps v1 is an upside-down BGR24 */
275  for (y = 0; y<avctx->height; y++)
276  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
277  &buf[y * avctx->width * 3],
278  3 * avctx->width);
279  }
280  break;
281 
282  case 2:
283  case 4:
284  /**
285  * Fraps v2 is Huffman-coded YUV420 planes
286  * Fraps v4 is virtually the same
287  */
288  for (i = 0; i < planes; i++) {
289  is_chroma = !!i;
290  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
291  avctx->width >> is_chroma,
292  avctx->height >> is_chroma,
293  buf + offs[i], offs[i + 1] - offs[i],
294  is_chroma, 1)) < 0) {
295  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
296  return ret;
297  }
298  }
299  break;
300  case 3:
301  case 5:
302  /* Virtually the same as version 4, but is for RGB24 */
303  for (i = 0; i < planes; i++) {
304  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
305  -f->linesize[0], avctx->width, avctx->height,
306  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
307  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
308  return ret;
309  }
310  }
311  out = f->data[0];
312  // convert pseudo-YUV into real RGB
313  for (j = 0; j < avctx->height; j++) {
314  uint8_t *line_end = out + 3*avctx->width;
315  while (out < line_end) {
316  out[0] += out[1];
317  out[2] += out[1];
318  out += 3;
319  }
320  out += f->linesize[0] - 3*avctx->width;
321  }
322  break;
323  }
324 
325  *got_frame = 1;
326 
327  return buf_size;
328 }
329 
330 
331 /**
332  * closes decoder
333  * @param avctx codec context
334  * @return 0 on success or negative if fails
335  */
337 {
338  FrapsContext *s = (FrapsContext*)avctx->priv_data;
339 
340  av_freep(&s->tmpbuf);
341  return 0;
342 }
343 
344 
346  .name = "fraps",
347  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
348  .type = AVMEDIA_TYPE_VIDEO,
349  .id = AV_CODEC_ID_FRAPS,
350  .priv_data_size = sizeof(FrapsContext),
351  .init = decode_init,
352  .close = decode_end,
353  .decode = decode_frame,
355  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
356 };
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:514
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
BswapDSPContext bdsp
Definition: fraps.c:54
version
Definition: libkvazaar.c:317
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int ff_huff_build_tree(AVCodecContext *avctx, 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:157
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1166
int size
Definition: packet.h:364
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:36
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
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:72
AVCodec.
Definition: codec.h:190
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
Definition: agm.c:913
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:215
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
#define av_cold
Definition: attributes.h:88
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
#define f(width, name)
Definition: cbs_vp9.c:255
Multithreading support functions.
uint8_t * data
Definition: packet.h:363
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:100
static const uint8_t header[24]
Definition: sdr2.c:67
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 av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition: huffman.h:39
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
const char * name
Name of the codec implementation.
Definition: codec.h:197
GLsizei count
Definition: opengl_enc.c:108
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:106
Definition: vlc.h:26
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 AVCodec caps_internal and use ff_thread_get_buffer() to allocate frames.The frames must then be freed with ff_thread_release_buffer().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
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
#define b
Definition: input.c:41
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:78
int width
picture width / height.
Definition: avcodec.h:704
uint8_t w
Definition: llviddspenc.c:38
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
#define VLC_BITS
Definition: fraps.c:47
Full range content.
Definition: pixfmt.h:586
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: fraps.c:136
AVCodecContext * avctx
Definition: fraps.c:53
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition: fraps.c:336
main external API structure.
Definition: avcodec.h:531
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition: fraps.c:65
static const struct @324 planes[]
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1159
huffman tree builder and VLC generator
int16_t sym
Definition: huffman.h:33
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:104
common internal api header.
AVCodec ff_fraps_decoder
Definition: fraps.c:345
uint8_t * tmpbuf
Definition: fraps.c:55
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:558
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
local variable storage
Definition: fraps.c:52
uint32_t count
Definition: huffman.h:35
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define stride
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
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:216
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
This structure stores compressed data.
Definition: packet.h:340
int tmpbuf_size
Definition: fraps.c:56
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:365
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
for(j=16;j >0;--j)
int i
Definition: input.c:407
#define FPS_TAG
Definition: fraps.c:46
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