FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avcodec.h"
35 #include "get_bits.h"
36 #include "huffman.h"
37 #include "bytestream.h"
38 #include "bswapdsp.h"
39 #include "internal.h"
40 #include "thread.h"
41 
42 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
43 #define VLC_BITS 11
44 
45 /**
46  * local variable storage
47  */
48 typedef struct FrapsContext {
53 } FrapsContext;
54 
55 
56 /**
57  * initializes decoder
58  * @param avctx codec context
59  * @return 0 on success or negative if fails
60  */
62 {
63  FrapsContext * const s = avctx->priv_data;
64 
65  s->avctx = avctx;
66  s->tmpbuf = NULL;
67 
69 
70  return 0;
71 }
72 
73 /**
74  * Comparator - our nodes should ascend by count
75  * but with preserved symbol order
76  */
77 static int huff_cmp(const void *va, const void *vb)
78 {
79  const Node *a = va, *b = vb;
80  return (a->count - b->count)*256 + a->sym - b->sym;
81 }
82 
83 /**
84  * decode Fraps v2 packed plane
85  */
86 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
87  int h, const uint8_t *src, int size, int Uoff,
88  const int step)
89 {
90  int i, j, ret;
91  GetBitContext gb;
92  VLC vlc;
93  Node nodes[512];
94 
95  for (i = 0; i < 256; i++)
96  nodes[i].count = bytestream_get_le32(&src);
97  size -= 1024;
98  if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
99  nodes, huff_cmp,
101  return ret;
102  /* we have built Huffman table and are ready to decode plane */
103 
104  /* convert bits so they may be used by standard bitreader */
105  s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
106  (const uint32_t *) src, size >> 2);
107 
108  if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
109  return ret;
110 
111  for (j = 0; j < h; j++) {
112  for (i = 0; i < w*step; i += step) {
113  dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
114  /* lines are stored as deltas between previous lines
115  * and we need to add 0x80 to the first lines of chroma planes
116  */
117  if (j)
118  dst[i] += dst[i - stride];
119  else if (Uoff)
120  dst[i] += 0x80;
121  if (get_bits_left(&gb) < 0) {
122  ff_free_vlc(&vlc);
123  return AVERROR_INVALIDDATA;
124  }
125  }
126  dst += stride;
127  }
128  ff_free_vlc(&vlc);
129  return 0;
130 }
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  const uint8_t *buf = avpkt->data;
138  int buf_size = avpkt->size;
139  ThreadFrame frame = { .f = data };
140  AVFrame * const f = data;
141  uint32_t header;
142  unsigned int version,header_size;
143  unsigned int x, y;
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  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  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
159 
160  if (version > 5) {
161  av_log(avctx, AV_LOG_ERROR,
162  "This file is encoded with Fraps version %d. " \
163  "This codec can only decode versions <= 5.\n", version);
164  return AVERROR_PATCHWELCOME;
165  }
166 
167  buf += header_size;
168 
169  if (version < 2) {
170  unsigned needed_size = avctx->width * avctx->height * 3;
171  if (version == 0) needed_size /= 2;
172  needed_size += header_size;
173  /* bit 31 means same as previous pic */
174  if (header & (1U<<31)) {
175  *got_frame = 0;
176  return buf_size;
177  }
178  if (buf_size != needed_size) {
179  av_log(avctx, AV_LOG_ERROR,
180  "Invalid frame length %d (should be %d)\n",
181  buf_size, needed_size);
182  return AVERROR_INVALIDDATA;
183  }
184  } else {
185  /* skip frame */
186  if (buf_size == 8) {
187  *got_frame = 0;
188  return buf_size;
189  }
190  if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
191  av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
192  return AVERROR_INVALIDDATA;
193  }
194  for (i = 0; i < planes; i++) {
195  offs[i] = AV_RL32(buf + 4 + i * 4);
196  if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
197  av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
198  return AVERROR_INVALIDDATA;
199  }
200  }
201  offs[planes] = buf_size - header_size;
202  for (i = 0; i < planes; i++) {
203  av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
204  if (!s->tmpbuf)
205  return AVERROR(ENOMEM);
206  }
207  }
208 
210  f->key_frame = 1;
211 
212  avctx->pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
213  avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
215  avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
216 
217  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
218  return ret;
219 
220  switch (version) {
221  case 0:
222  default:
223  /* Fraps v0 is a reordered YUV420 */
224  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
225  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
226  avctx->width, avctx->height);
227  return AVERROR_INVALIDDATA;
228  }
229 
230  buf32 = (const uint32_t*)buf;
231  for (y = 0; y < avctx->height / 2; y++) {
232  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
233  luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
234  cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
235  cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
236  for (x = 0; x < avctx->width; x += 8) {
237  *luma1++ = *buf32++;
238  *luma1++ = *buf32++;
239  *luma2++ = *buf32++;
240  *luma2++ = *buf32++;
241  *cr++ = *buf32++;
242  *cb++ = *buf32++;
243  }
244  }
245  break;
246 
247  case 1:
248  /* Fraps v1 is an upside-down BGR24 */
249  for (y = 0; y<avctx->height; y++)
250  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
251  &buf[y * avctx->width * 3],
252  3 * avctx->width);
253  break;
254 
255  case 2:
256  case 4:
257  /**
258  * Fraps v2 is Huffman-coded YUV420 planes
259  * Fraps v4 is virtually the same
260  */
261  for (i = 0; i < planes; i++) {
262  is_chroma = !!i;
263  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
264  avctx->width >> is_chroma,
265  avctx->height >> is_chroma,
266  buf + offs[i], offs[i + 1] - offs[i],
267  is_chroma, 1)) < 0) {
268  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
269  return ret;
270  }
271  }
272  break;
273  case 3:
274  case 5:
275  /* Virtually the same as version 4, but is for RGB24 */
276  for (i = 0; i < planes; i++) {
277  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
278  -f->linesize[0], avctx->width, avctx->height,
279  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
280  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
281  return ret;
282  }
283  }
284  out = f->data[0];
285  // convert pseudo-YUV into real RGB
286  for (j = 0; j < avctx->height; j++) {
287  uint8_t *line_end = out + 3*avctx->width;
288  while (out < line_end) {
289  out[0] += out[1];
290  out[2] += out[1];
291  out += 3;
292  }
293  out += f->linesize[0] - 3*avctx->width;
294  }
295  break;
296  }
297 
298  *got_frame = 1;
299 
300  return buf_size;
301 }
302 
303 
304 /**
305  * closes decoder
306  * @param avctx codec context
307  * @return 0 on success or negative if fails
308  */
310 {
311  FrapsContext *s = (FrapsContext*)avctx->priv_data;
312 
313  av_freep(&s->tmpbuf);
314  return 0;
315 }
316 
317 
319  .name = "fraps",
320  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
321  .type = AVMEDIA_TYPE_VIDEO,
322  .id = AV_CODEC_ID_FRAPS,
323  .priv_data_size = sizeof(FrapsContext),
324  .init = decode_init,
325  .close = decode_end,
326  .decode = decode_frame,
328 };
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:438
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
BswapDSPContext bdsp
Definition: fraps.c:50
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:2413
int size
Definition: avcodec.h:1602
const char * b
Definition: vf_curves.c:113
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
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:120
int version
Definition: avisynth_c.h:766
AVCodec.
Definition: avcodec.h:3600
Definition: huffman.h:32
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:97
uint8_t
#define av_cold
Definition: attributes.h:82
Multithreading support functions.
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1601
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
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:86
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:568
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition: huffman.h:39
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
GLsizei count
Definition: opengl_enc.c:109
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1022
Definition: vlc.h:26
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:77
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:258
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:74
int width
picture width / height.
Definition: avcodec.h:1863
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:535
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
#define src
Definition: vp9dsp.c:530
#define VLC_BITS
Definition: fraps.c:43
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:460
#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:132
AVCodecContext * avctx
Definition: fraps.c:49
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:437
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition: fraps.c:309
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1676
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition: fraps.c:61
void * buf
Definition: avisynth_c.h:690
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2406
huffman tree builder and VLC generator
int16_t sym
Definition: huffman.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal api header.
AVCodec ff_fraps_decoder
Definition: fraps.c:318
uint8_t * tmpbuf
Definition: fraps.c:51
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1718
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:253
local variable storage
Definition: fraps.c:48
uint32_t count
Definition: huffman.h:35
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2035
#define stride
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:98
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1578
int tmpbuf_size
Definition: fraps.c:52
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:360
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
for(j=16;j >0;--j)
#define FPS_TAG
Definition: fraps.c:42