Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
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 
41 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
42 #define VLC_BITS 11
43 
47 typedef struct FrapsContext {
53 } FrapsContext;
54 
55 
62 {
63  FrapsContext * const s = avctx->priv_data;
64 
65  avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
66 
67  s->avctx = avctx;
68  s->tmpbuf = NULL;
69 
70  s->frame = av_frame_alloc();
71  if (!s->frame)
72  return AVERROR(ENOMEM);
73 
75 
76  return 0;
77 }
78 
83 static int huff_cmp(const void *va, const void *vb)
84 {
85  const Node *a = va, *b = vb;
86  return (a->count - b->count)*256 + a->sym - b->sym;
87 }
88 
92 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
93  int h, const uint8_t *src, int size, int Uoff,
94  const int step)
95 {
96  int i, j, ret;
97  GetBitContext gb;
98  VLC vlc;
99  Node nodes[512];
100 
101  for (i = 0; i < 256; i++)
102  nodes[i].count = bytestream_get_le32(&src);
103  size -= 1024;
104  if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
105  nodes, huff_cmp,
107  return ret;
108  /* we have built Huffman table and are ready to decode plane */
109 
110  /* convert bits so they may be used by standard bitreader */
111  s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
112  (const uint32_t *) src, size >> 2);
113 
114  init_get_bits(&gb, s->tmpbuf, size * 8);
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  AVFrame *frame = data;
144  AVFrame * const f = s->frame;
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, planes;
152  enum AVPixelFormat pix_fmt;
153  int prev_pic_bit, expected_size;
154 
155  if (buf_size < 4) {
156  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
157  return AVERROR_INVALIDDATA;
158  }
159 
160  header = AV_RL32(buf);
161  version = header & 0xff;
162  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
163  prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
164 
165  if (version > 5) {
166  av_log(avctx, AV_LOG_ERROR,
167  "This file is encoded with Fraps version %d. " \
168  "This codec can only decode versions <= 5.\n", version);
169  return AVERROR_PATCHWELCOME;
170  }
171 
172  buf += 4;
173  if (header_size == 8)
174  buf += 4;
175 
176  pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
177  if (avctx->pix_fmt != pix_fmt && f->data[0]) {
178  av_frame_unref(f);
179  }
180  avctx->pix_fmt = pix_fmt;
181  avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
183 
184  expected_size = header_size;
185 
186  switch (version) {
187  case 0:
188  default:
189  /* Fraps v0 is a reordered YUV420 */
190  if (!prev_pic_bit)
191  expected_size += avctx->width * avctx->height * 3 / 2;
192  if (buf_size != expected_size) {
193  av_log(avctx, AV_LOG_ERROR,
194  "Invalid frame length %d (should be %d)\n",
195  buf_size, expected_size);
196  return AVERROR_INVALIDDATA;
197  }
198 
199  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
200  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
201  avctx->width, avctx->height);
202  return AVERROR_INVALIDDATA;
203  }
204 
205  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
206  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
207  return ret;
208  }
209  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
211 
212  if (f->pict_type == AV_PICTURE_TYPE_I) {
213  buf32 = (const uint32_t*)buf;
214  for (y = 0; y < avctx->height / 2; y++) {
215  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
216  luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
217  cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
218  cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
219  for (x = 0; x < avctx->width; x += 8) {
220  *(luma1++) = *(buf32++);
221  *(luma1++) = *(buf32++);
222  *(luma2++) = *(buf32++);
223  *(luma2++) = *(buf32++);
224  *(cr++) = *(buf32++);
225  *(cb++) = *(buf32++);
226  }
227  }
228  }
229  break;
230 
231  case 1:
232  /* Fraps v1 is an upside-down BGR24 */
233  if (!prev_pic_bit)
234  expected_size += avctx->width * avctx->height * 3;
235  if (buf_size != expected_size) {
236  av_log(avctx, AV_LOG_ERROR,
237  "Invalid frame length %d (should be %d)\n",
238  buf_size, expected_size);
239  return AVERROR_INVALIDDATA;
240  }
241 
242  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
243  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
244  return ret;
245  }
246  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
248 
249  if (f->pict_type == AV_PICTURE_TYPE_I) {
250  for (y = 0; y<avctx->height; y++)
251  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
252  &buf[y * avctx->width * 3],
253  3 * avctx->width);
254  }
255  break;
256 
257  case 2:
258  case 4:
263  planes = 3;
264  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
265  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
266  return ret;
267  }
268  /* skip frame */
269  if (buf_size == 8) {
271  f->key_frame = 0;
272  break;
273  }
275  f->key_frame = 1;
276  if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
277  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
278  return AVERROR_INVALIDDATA;
279  }
280  for (i = 0; i < planes; i++) {
281  offs[i] = AV_RL32(buf + 4 + i * 4);
282  if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
283  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
284  return AVERROR_INVALIDDATA;
285  }
286  }
287  offs[planes] = buf_size;
288  for (i = 0; i < planes; i++) {
289  is_chroma = !!i;
291  offs[i + 1] - offs[i] - 1024);
292  if (!s->tmpbuf)
293  return AVERROR(ENOMEM);
294  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
295  avctx->width >> is_chroma,
296  avctx->height >> is_chroma,
297  buf + offs[i], offs[i + 1] - offs[i],
298  is_chroma, 1)) < 0) {
299  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
300  return ret;
301  }
302  }
303  break;
304  case 3:
305  case 5:
306  /* Virtually the same as version 4, but is for RGB24 */
307  planes = 3;
308  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
309  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
310  return ret;
311  }
312  /* skip frame */
313  if (buf_size == 8) {
315  f->key_frame = 0;
316  break;
317  }
319  f->key_frame = 1;
320  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
321  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
322  return AVERROR_INVALIDDATA;
323  }
324  for (i = 0; i < planes; i++) {
325  offs[i] = AV_RL32(buf + 4 + i * 4);
326  if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
327  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
328  return AVERROR_INVALIDDATA;
329  }
330  }
331  offs[planes] = buf_size;
332  for (i = 0; i < planes; i++) {
334  offs[i + 1] - offs[i] - 1024);
335  if (!s->tmpbuf)
336  return AVERROR(ENOMEM);
337  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
338  -f->linesize[0], avctx->width, avctx->height,
339  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
340  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
341  return ret;
342  }
343  }
344  // convert pseudo-YUV into real RGB
345  for (j = 0; j < avctx->height; j++) {
346  for (i = 0; i < avctx->width; i++) {
347  f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
348  f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
349  }
350  }
351  break;
352  }
353 
354  if ((ret = av_frame_ref(frame, f)) < 0)
355  return ret;
356  *got_frame = 1;
357 
358  return buf_size;
359 }
360 
361 
368 {
369  FrapsContext *s = (FrapsContext*)avctx->priv_data;
370 
371  av_frame_free(&s->frame);
372 
373  av_freep(&s->tmpbuf);
374  return 0;
375 }
376 
377 
379  .name = "fraps",
380  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
381  .type = AVMEDIA_TYPE_VIDEO,
382  .id = AV_CODEC_ID_FRAPS,
383  .priv_data_size = sizeof(FrapsContext),
384  .init = decode_init,
385  .close = decode_end,
386  .decode = decode_frame,
387  .capabilities = AV_CODEC_CAP_DR1,
388 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
BswapDSPContext bdsp
Definition: fraps.c:49
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
Definition: vf_drawbox.c:37
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:137
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2127
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer with padding, reusing the given one if large enough.
Definition: utils.c:60
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3120
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Definition: huffman.h:32
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
#define b
Definition: input.c:52
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:199
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
bitstream reader API header.
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:92
#define src
Definition: vp8dsp.c:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition: huffman.h:39
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
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:83
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: utils.c:672
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
int width
picture width / height.
Definition: avcodec.h:1580
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
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:493
#define AV_RL32
Definition: intreadwrite.h:146
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
enum AVPixelFormat pix_fmt
Definition: movenc.c:853
#define VLC_BITS
Definition: fraps.c:42
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:365
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: fraps.c:136
AVCodecContext * avctx
Definition: fraps.c:48
NULL
Definition: eval.c:55
Libavcodec external API header.
version
Definition: ffv1enc.c:1091
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition: fraps.c:367
main external API structure.
Definition: avcodec.h:1409
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition: fraps.c:61
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
huffman tree builder and VLC generator
static int step
Definition: avplay.c:247
int16_t sym
Definition: huffman.h:33
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:302
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
common internal api header.
AVCodec ff_fraps_decoder
Definition: fraps.c:378
uint8_t * tmpbuf
Definition: fraps.c:51
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
void * priv_data
Definition: avcodec.h:1451
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
local variable storage
Definition: fraps.c:47
uint32_t count
Definition: huffman.h:35
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
AVFrame * frame
Definition: fraps.c:50
This structure stores compressed data.
Definition: avcodec.h:1323
int tmpbuf_size
Definition: fraps.c:52
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:334
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:261
#define FPS_TAG
Definition: fraps.c:41