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 
46 typedef struct FrapsContext {
52 } FrapsContext;
53 
54 
61 {
62  FrapsContext * const s = avctx->priv_data;
63 
64  avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
65 
66  s->avctx = avctx;
67  s->tmpbuf = NULL;
68 
69  s->frame = av_frame_alloc();
70  if (!s->frame)
71  return AVERROR(ENOMEM);
72 
74 
75  return 0;
76 }
77 
82 static int huff_cmp(const void *va, const void *vb)
83 {
84  const Node *a = va, *b = vb;
85  return (a->count - b->count)*256 + a->sym - b->sym;
86 }
87 
91 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
92  int h, const uint8_t *src, int size, int Uoff,
93  const int step)
94 {
95  int i, j, ret;
96  GetBitContext gb;
97  VLC vlc;
98  Node nodes[512];
99 
100  for (i = 0; i < 256; i++)
101  nodes[i].count = bytestream_get_le32(&src);
102  size -= 1024;
103  if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, 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  init_get_bits(&gb, s->tmpbuf, size * 8);
113  for (j = 0; j < h; j++) {
114  for (i = 0; i < w*step; i += step) {
115  dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
116  /* lines are stored as deltas between previous lines
117  * and we need to add 0x80 to the first lines of chroma planes
118  */
119  if (j)
120  dst[i] += dst[i - stride];
121  else if (Uoff)
122  dst[i] += 0x80;
123  if (get_bits_left(&gb) < 0) {
124  ff_free_vlc(&vlc);
125  return AVERROR_INVALIDDATA;
126  }
127  }
128  dst += stride;
129  }
130  ff_free_vlc(&vlc);
131  return 0;
132 }
133 
134 static int decode_frame(AVCodecContext *avctx,
135  void *data, int *got_frame,
136  AVPacket *avpkt)
137 {
138  FrapsContext * const s = avctx->priv_data;
139  const uint8_t *buf = avpkt->data;
140  int buf_size = avpkt->size;
141  AVFrame *frame = data;
142  AVFrame * const f = s->frame;
143  uint32_t header;
144  unsigned int version,header_size;
145  unsigned int x, y;
146  const uint32_t *buf32;
147  uint32_t *luma1,*luma2,*cb,*cr;
148  uint32_t offs[4];
149  int i, j, ret, is_chroma, planes;
150  enum AVPixelFormat pix_fmt;
151  int prev_pic_bit, expected_size;
152 
153  if (buf_size < 4) {
154  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
155  return AVERROR_INVALIDDATA;
156  }
157 
158  header = AV_RL32(buf);
159  version = header & 0xff;
160  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
161  prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
162 
163  if (version > 5) {
164  av_log(avctx, AV_LOG_ERROR,
165  "This file is encoded with Fraps version %d. " \
166  "This codec can only decode versions <= 5.\n", version);
167  return AVERROR_PATCHWELCOME;
168  }
169 
170  buf += 4;
171  if (header_size == 8)
172  buf += 4;
173 
174  pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
175  if (avctx->pix_fmt != pix_fmt && f->data[0]) {
176  av_frame_unref(f);
177  }
178  avctx->pix_fmt = pix_fmt;
179  avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
181 
182  expected_size = header_size;
183 
184  switch (version) {
185  case 0:
186  default:
187  /* Fraps v0 is a reordered YUV420 */
188  if (!prev_pic_bit)
189  expected_size += avctx->width * avctx->height * 3 / 2;
190  if (buf_size != expected_size) {
191  av_log(avctx, AV_LOG_ERROR,
192  "Invalid frame length %d (should be %d)\n",
193  buf_size, expected_size);
194  return AVERROR_INVALIDDATA;
195  }
196 
197  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
198  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
199  avctx->width, avctx->height);
200  return AVERROR_INVALIDDATA;
201  }
202 
203  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
204  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
205  return ret;
206  }
207  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
209 
210  if (f->pict_type == AV_PICTURE_TYPE_I) {
211  buf32 = (const uint32_t*)buf;
212  for (y = 0; y < avctx->height / 2; y++) {
213  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
214  luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
215  cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
216  cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
217  for (x = 0; x < avctx->width; x += 8) {
218  *(luma1++) = *(buf32++);
219  *(luma1++) = *(buf32++);
220  *(luma2++) = *(buf32++);
221  *(luma2++) = *(buf32++);
222  *(cr++) = *(buf32++);
223  *(cb++) = *(buf32++);
224  }
225  }
226  }
227  break;
228 
229  case 1:
230  /* Fraps v1 is an upside-down BGR24 */
231  if (!prev_pic_bit)
232  expected_size += avctx->width * avctx->height * 3;
233  if (buf_size != expected_size) {
234  av_log(avctx, AV_LOG_ERROR,
235  "Invalid frame length %d (should be %d)\n",
236  buf_size, expected_size);
237  return AVERROR_INVALIDDATA;
238  }
239 
240  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
241  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
242  return ret;
243  }
244  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
246 
247  if (f->pict_type == AV_PICTURE_TYPE_I) {
248  for (y = 0; y<avctx->height; y++)
249  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
250  &buf[y * avctx->width * 3],
251  3 * avctx->width);
252  }
253  break;
254 
255  case 2:
256  case 4:
261  planes = 3;
262  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
263  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
264  return ret;
265  }
266  /* skip frame */
267  if (buf_size == 8) {
269  f->key_frame = 0;
270  break;
271  }
273  f->key_frame = 1;
274  if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
275  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
276  return AVERROR_INVALIDDATA;
277  }
278  for (i = 0; i < planes; i++) {
279  offs[i] = AV_RL32(buf + 4 + i * 4);
280  if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
281  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
282  return AVERROR_INVALIDDATA;
283  }
284  }
285  offs[planes] = buf_size;
286  for (i = 0; i < planes; i++) {
287  is_chroma = !!i;
289  offs[i + 1] - offs[i] - 1024);
290  if (!s->tmpbuf)
291  return AVERROR(ENOMEM);
292  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
293  avctx->width >> is_chroma,
294  avctx->height >> is_chroma,
295  buf + offs[i], offs[i + 1] - offs[i],
296  is_chroma, 1)) < 0) {
297  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
298  return ret;
299  }
300  }
301  break;
302  case 3:
303  case 5:
304  /* Virtually the same as version 4, but is for RGB24 */
305  planes = 3;
306  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
307  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
308  return ret;
309  }
310  /* skip frame */
311  if (buf_size == 8) {
313  f->key_frame = 0;
314  break;
315  }
317  f->key_frame = 1;
318  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
319  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
320  return AVERROR_INVALIDDATA;
321  }
322  for (i = 0; i < planes; i++) {
323  offs[i] = AV_RL32(buf + 4 + i * 4);
324  if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
325  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
326  return AVERROR_INVALIDDATA;
327  }
328  }
329  offs[planes] = buf_size;
330  for (i = 0; i < planes; i++) {
332  offs[i + 1] - offs[i] - 1024);
333  if (!s->tmpbuf)
334  return AVERROR(ENOMEM);
335  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
336  -f->linesize[0], avctx->width, avctx->height,
337  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
338  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
339  return ret;
340  }
341  }
342  // convert pseudo-YUV into real RGB
343  for (j = 0; j < avctx->height; j++) {
344  for (i = 0; i < avctx->width; i++) {
345  f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
346  f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
347  }
348  }
349  break;
350  }
351 
352  if ((ret = av_frame_ref(frame, f)) < 0)
353  return ret;
354  *got_frame = 1;
355 
356  return buf_size;
357 }
358 
359 
366 {
367  FrapsContext *s = (FrapsContext*)avctx->priv_data;
368 
369  av_frame_free(&s->frame);
370 
371  av_freep(&s->tmpbuf);
372  return 0;
373 }
374 
375 
377  .name = "fraps",
378  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
379  .type = AVMEDIA_TYPE_VIDEO,
380  .id = AV_CODEC_ID_FRAPS,
381  .priv_data_size = sizeof(FrapsContext),
382  .init = decode_init,
383  .close = decode_end,
384  .decode = decode_frame,
385  .capabilities = CODEC_CAP_DR1,
386 };
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
BswapDSPContext bdsp
Definition: fraps.c:48
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1782
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
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:59
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2812
int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
Definition: huffman.c:135
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:198
Definition: huffman.h:32
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
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:57
#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:188
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
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:91
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#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:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
Definition: get_bits.h:64
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
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:82
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:808
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
int width
picture width / height.
Definition: avcodec.h:1229
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:522
#define AV_RL32
Definition: intreadwrite.h:146
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
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:134
AVCodecContext * avctx
Definition: fraps.c:47
NULL
Definition: eval.c:55
Libavcodec external API header.
version
Definition: ffv1enc.c:1080
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition: fraps.c:365
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition: fraps.c:60
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
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:283
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
common internal api header.
AVCodec ff_fraps_decoder
Definition: fraps.c:376
uint8_t * tmpbuf
Definition: fraps.c:50
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1092
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:66
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
local variable storage
Definition: fraps.c:46
uint32_t count
Definition: huffman.h:35
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
AVFrame * frame
Definition: fraps.c:49
This structure stores compressed data.
Definition: avcodec.h:950
int tmpbuf_size
Definition: fraps.c:51
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:333
for(j=16;j >0;--j)
Predicted.
Definition: avutil.h:254
#define FPS_TAG
Definition: fraps.c:41
Definition: vf_drawbox.c:37