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 "dsputil.h"
39 #include "internal.h"
40 #include "thread.h"
41 
42 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
43 
44 /**
45  * local variable storage
46  */
47 typedef struct FrapsContext {
52 } FrapsContext;
53 
54 
55 /**
56  * initializes decoder
57  * @param avctx codec context
58  * @return 0 on success or negative if fails
59  */
61 {
62  FrapsContext * const s = avctx->priv_data;
63 
64  s->avctx = avctx;
65  s->tmpbuf = NULL;
66 
67  ff_dsputil_init(&s->dsp, avctx);
68 
69  return 0;
70 }
71 
72 /**
73  * Comparator - our nodes should ascend by count
74  * but with preserved symbol order
75  */
76 static int huff_cmp(const void *va, const void *vb)
77 {
78  const Node *a = va, *b = vb;
79  return (a->count - b->count)*256 + a->sym - b->sym;
80 }
81 
82 /**
83  * decode Fraps v2 packed plane
84  */
85 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
86  int h, const uint8_t *src, int size, int Uoff,
87  const int step)
88 {
89  int i, j, ret;
90  GetBitContext gb;
91  VLC vlc;
92  Node nodes[512];
93 
94  for (i = 0; i < 256; i++)
95  nodes[i].count = bytestream_get_le32(&src);
96  size -= 1024;
97  if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
99  return ret;
100  /* we have built Huffman table and are ready to decode plane */
101 
102  /* convert bits so they may be used by standard bitreader */
103  s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
104 
105  init_get_bits(&gb, s->tmpbuf, size * 8);
106  for (j = 0; j < h; j++) {
107  for (i = 0; i < w*step; i += step) {
108  dst[i] = get_vlc2(&gb, vlc.table, FF_HUFFMAN_BITS, 3);
109  /* lines are stored as deltas between previous lines
110  * and we need to add 0x80 to the first lines of chroma planes
111  */
112  if (j)
113  dst[i] += dst[i - stride];
114  else if (Uoff)
115  dst[i] += 0x80;
116  if (get_bits_left(&gb) < 0) {
117  ff_free_vlc(&vlc);
118  return AVERROR_INVALIDDATA;
119  }
120  }
121  dst += stride;
122  }
123  ff_free_vlc(&vlc);
124  return 0;
125 }
126 
127 static int decode_frame(AVCodecContext *avctx,
128  void *data, int *got_frame,
129  AVPacket *avpkt)
130 {
131  FrapsContext * const s = avctx->priv_data;
132  const uint8_t *buf = avpkt->data;
133  int buf_size = avpkt->size;
134  ThreadFrame frame = { .f = data };
135  AVFrame * const f = data;
136  uint32_t header;
137  unsigned int version,header_size;
138  unsigned int x, y;
139  const uint32_t *buf32;
140  uint32_t *luma1,*luma2,*cb,*cr;
141  uint32_t offs[4];
142  int i, j, ret, is_chroma;
143  const int planes = 3;
144  uint8_t *out;
145 
146  header = AV_RL32(buf);
147  version = header & 0xff;
148  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
149 
150  if (version > 5) {
151  av_log(avctx, AV_LOG_ERROR,
152  "This file is encoded with Fraps version %d. " \
153  "This codec can only decode versions <= 5.\n", version);
154  return AVERROR_PATCHWELCOME;
155  }
156 
157  buf += header_size;
158 
159  if (version < 2) {
160  unsigned needed_size = avctx->width * avctx->height * 3;
161  if (version == 0) needed_size /= 2;
162  needed_size += header_size;
163  /* bit 31 means same as previous pic */
164  if (header & (1U<<31)) {
165  *got_frame = 0;
166  return buf_size;
167  }
168  if (buf_size != needed_size) {
169  av_log(avctx, AV_LOG_ERROR,
170  "Invalid frame length %d (should be %d)\n",
171  buf_size, needed_size);
172  return AVERROR_INVALIDDATA;
173  }
174  } else {
175  /* skip frame */
176  if (buf_size == 8) {
177  *got_frame = 0;
178  return buf_size;
179  }
180  if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
181  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
182  return AVERROR_INVALIDDATA;
183  }
184  for (i = 0; i < planes; i++) {
185  offs[i] = AV_RL32(buf + 4 + i * 4);
186  if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
187  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
188  return AVERROR_INVALIDDATA;
189  }
190  }
191  offs[planes] = buf_size - header_size;
192  for (i = 0; i < planes; i++) {
193  av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
194  if (!s->tmpbuf)
195  return AVERROR(ENOMEM);
196  }
197  }
198 
200  f->key_frame = 1;
201 
202  avctx->pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
203  avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED : AVCOL_RANGE_JPEG;
204 
205  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
206  return ret;
207 
208  switch (version) {
209  case 0:
210  default:
211  /* Fraps v0 is a reordered YUV420 */
212  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
213  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
214  avctx->width, avctx->height);
215  return AVERROR_INVALIDDATA;
216  }
217 
218  buf32 = (const uint32_t*)buf;
219  for (y = 0; y < avctx->height / 2; y++) {
220  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
221  luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
222  cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
223  cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
224  for (x = 0; x < avctx->width; x += 8) {
225  *luma1++ = *buf32++;
226  *luma1++ = *buf32++;
227  *luma2++ = *buf32++;
228  *luma2++ = *buf32++;
229  *cr++ = *buf32++;
230  *cb++ = *buf32++;
231  }
232  }
233  break;
234 
235  case 1:
236  /* Fraps v1 is an upside-down BGR24 */
237  for (y = 0; y<avctx->height; y++)
238  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
239  &buf[y * avctx->width * 3],
240  3 * avctx->width);
241  break;
242 
243  case 2:
244  case 4:
245  /**
246  * Fraps v2 is Huffman-coded YUV420 planes
247  * Fraps v4 is virtually the same
248  */
249  for (i = 0; i < planes; i++) {
250  is_chroma = !!i;
251  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
252  avctx->width >> is_chroma,
253  avctx->height >> is_chroma,
254  buf + offs[i], offs[i + 1] - offs[i],
255  is_chroma, 1)) < 0) {
256  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
257  return ret;
258  }
259  }
260  break;
261  case 3:
262  case 5:
263  /* Virtually the same as version 4, but is for RGB24 */
264  for (i = 0; i < planes; i++) {
265  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
266  -f->linesize[0], avctx->width, avctx->height,
267  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
268  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
269  return ret;
270  }
271  }
272  out = f->data[0];
273  // convert pseudo-YUV into real RGB
274  for (j = 0; j < avctx->height; j++) {
275  uint8_t *line_end = out + 3*avctx->width;
276  while (out < line_end) {
277  out[0] += out[1];
278  out[2] += out[1];
279  out += 3;
280  }
281  out += f->linesize[0] - 3*avctx->width;
282  }
283  break;
284  }
285 
286  *got_frame = 1;
287 
288  return buf_size;
289 }
290 
291 
292 /**
293  * closes decoder
294  * @param avctx codec context
295  * @return 0 on success or negative if fails
296  */
298 {
299  FrapsContext *s = (FrapsContext*)avctx->priv_data;
300 
301  av_freep(&s->tmpbuf);
302  return 0;
303 }
304 
305 
307  .name = "fraps",
308  .type = AVMEDIA_TYPE_VIDEO,
309  .id = AV_CODEC_ID_FRAPS,
310  .priv_data_size = sizeof(FrapsContext),
311  .init = decode_init,
312  .close = decode_end,
313  .decode = decode_frame,
314  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
315  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
316 };