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 "dsputil.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 
73  ff_dsputil_init(&s->dsp, avctx);
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->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
110 
111  init_get_bits(&gb, s->tmpbuf, size * 8);
112  for (j = 0; j < h; j++) {
113  for (i = 0; i < w*step; i += step) {
114  dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
115  /* lines are stored as deltas between previous lines
116  * and we need to add 0x80 to the first lines of chroma planes
117  */
118  if (j)
119  dst[i] += dst[i - stride];
120  else if (Uoff)
121  dst[i] += 0x80;
122  if (get_bits_left(&gb) < 0) {
123  ff_free_vlc(&vlc);
124  return AVERROR_INVALIDDATA;
125  }
126  }
127  dst += stride;
128  }
129  ff_free_vlc(&vlc);
130  return 0;
131 }
132 
133 static int decode_frame(AVCodecContext *avctx,
134  void *data, int *got_frame,
135  AVPacket *avpkt)
136 {
137  FrapsContext * const s = avctx->priv_data;
138  const uint8_t *buf = avpkt->data;
139  int buf_size = avpkt->size;
140  AVFrame *frame = data;
141  AVFrame * const f = s->frame;
142  uint32_t header;
143  unsigned int version,header_size;
144  unsigned int x, y;
145  const uint32_t *buf32;
146  uint32_t *luma1,*luma2,*cb,*cr;
147  uint32_t offs[4];
148  int i, j, ret, is_chroma, planes;
149  enum AVPixelFormat pix_fmt;
150  int prev_pic_bit, expected_size;
151 
152  if (buf_size < 4) {
153  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
154  return AVERROR_INVALIDDATA;
155  }
156 
157  header = AV_RL32(buf);
158  version = header & 0xff;
159  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
160  prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
161 
162  if (version > 5) {
163  av_log(avctx, AV_LOG_ERROR,
164  "This file is encoded with Fraps version %d. " \
165  "This codec can only decode versions <= 5.\n", version);
166  return AVERROR_PATCHWELCOME;
167  }
168 
169  buf += 4;
170  if (header_size == 8)
171  buf += 4;
172 
173  pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
174  if (avctx->pix_fmt != pix_fmt && f->data[0]) {
175  av_frame_unref(f);
176  }
177  avctx->pix_fmt = pix_fmt;
178 
179  expected_size = header_size;
180 
181  switch (version) {
182  case 0:
183  default:
184  /* Fraps v0 is a reordered YUV420 */
185  if (!prev_pic_bit)
186  expected_size += avctx->width * avctx->height * 3 / 2;
187  if (buf_size != expected_size) {
188  av_log(avctx, AV_LOG_ERROR,
189  "Invalid frame length %d (should be %d)\n",
190  buf_size, expected_size);
191  return AVERROR_INVALIDDATA;
192  }
193 
194  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
195  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
196  avctx->width, avctx->height);
197  return AVERROR_INVALIDDATA;
198  }
199 
200  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
201  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
202  return ret;
203  }
204  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
206 
207  if (f->pict_type == AV_PICTURE_TYPE_I) {
208  buf32 = (const uint32_t*)buf;
209  for (y = 0; y < avctx->height / 2; y++) {
210  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
211  luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
212  cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
213  cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
214  for (x = 0; x < avctx->width; x += 8) {
215  *(luma1++) = *(buf32++);
216  *(luma1++) = *(buf32++);
217  *(luma2++) = *(buf32++);
218  *(luma2++) = *(buf32++);
219  *(cr++) = *(buf32++);
220  *(cb++) = *(buf32++);
221  }
222  }
223  }
224  break;
225 
226  case 1:
227  /* Fraps v1 is an upside-down BGR24 */
228  if (!prev_pic_bit)
229  expected_size += avctx->width * avctx->height * 3;
230  if (buf_size != expected_size) {
231  av_log(avctx, AV_LOG_ERROR,
232  "Invalid frame length %d (should be %d)\n",
233  buf_size, expected_size);
234  return AVERROR_INVALIDDATA;
235  }
236 
237  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
238  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
239  return ret;
240  }
241  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
243 
244  if (f->pict_type == AV_PICTURE_TYPE_I) {
245  for (y = 0; y<avctx->height; y++)
246  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
247  &buf[y * avctx->width * 3],
248  3 * avctx->width);
249  }
250  break;
251 
252  case 2:
253  case 4:
258  planes = 3;
259  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
260  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
261  return ret;
262  }
263  /* skip frame */
264  if (buf_size == 8) {
266  f->key_frame = 0;
267  break;
268  }
270  f->key_frame = 1;
271  if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
272  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
273  return AVERROR_INVALIDDATA;
274  }
275  for (i = 0; i < planes; i++) {
276  offs[i] = AV_RL32(buf + 4 + i * 4);
277  if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
278  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
279  return AVERROR_INVALIDDATA;
280  }
281  }
282  offs[planes] = buf_size;
283  for (i = 0; i < planes; i++) {
284  is_chroma = !!i;
286  offs[i + 1] - offs[i] - 1024);
287  if (!s->tmpbuf)
288  return AVERROR(ENOMEM);
289  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
290  avctx->width >> is_chroma,
291  avctx->height >> is_chroma,
292  buf + offs[i], offs[i + 1] - offs[i],
293  is_chroma, 1)) < 0) {
294  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
295  return ret;
296  }
297  }
298  break;
299  case 3:
300  case 5:
301  /* Virtually the same as version 4, but is for RGB24 */
302  planes = 3;
303  if ((ret = ff_reget_buffer(avctx, f)) < 0) {
304  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
305  return ret;
306  }
307  /* skip frame */
308  if (buf_size == 8) {
310  f->key_frame = 0;
311  break;
312  }
314  f->key_frame = 1;
315  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
316  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
317  return AVERROR_INVALIDDATA;
318  }
319  for (i = 0; i < planes; i++) {
320  offs[i] = AV_RL32(buf + 4 + i * 4);
321  if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
322  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
323  return AVERROR_INVALIDDATA;
324  }
325  }
326  offs[planes] = buf_size;
327  for (i = 0; i < planes; i++) {
329  offs[i + 1] - offs[i] - 1024);
330  if (!s->tmpbuf)
331  return AVERROR(ENOMEM);
332  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
333  -f->linesize[0], avctx->width, avctx->height,
334  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
335  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
336  return ret;
337  }
338  }
339  // convert pseudo-YUV into real RGB
340  for (j = 0; j < avctx->height; j++) {
341  for (i = 0; i < avctx->width; i++) {
342  f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
343  f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
344  }
345  }
346  break;
347  }
348 
349  if ((ret = av_frame_ref(frame, f)) < 0)
350  return ret;
351  *got_frame = 1;
352 
353  return buf_size;
354 }
355 
356 
363 {
364  FrapsContext *s = (FrapsContext*)avctx->priv_data;
365 
366  av_frame_free(&s->frame);
367 
368  av_freep(&s->tmpbuf);
369  return 0;
370 }
371 
372 
374  .name = "fraps",
375  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
376  .type = AVMEDIA_TYPE_VIDEO,
377  .id = AV_CODEC_ID_FRAPS,
378  .priv_data_size = sizeof(FrapsContext),
379  .init = decode_init,
380  .close = decode_end,
381  .decode = decode_frame,
382  .capabilities = CODEC_CAP_DR1,
383 };