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 
40 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
41 
45 typedef struct FrapsContext{
48  uint8_t *tmpbuf;
50 } FrapsContext;
51 
52 
59 {
60  FrapsContext * const s = avctx->priv_data;
61 
62  avctx->coded_frame = (AVFrame*)&s->frame;
63  avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
64 
65  s->avctx = avctx;
66  s->tmpbuf = NULL;
67 
68  dsputil_init(&s->dsp, avctx);
69 
70  return 0;
71 }
72 
77 static int huff_cmp(const void *va, const void *vb){
78  const Node *a = va, *b = vb;
79  return (a->count - b->count)*256 + a->sym - b->sym;
80 }
81 
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;
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 (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
99  return -1;
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, 9, 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) dst[i] += dst[i - stride];
113  else if(Uoff) dst[i] += 0x80;
114  if (get_bits_left(&gb) < 0) {
115  ff_free_vlc(&vlc);
116  return AVERROR_INVALIDDATA;
117  }
118  }
119  dst += stride;
120  }
121  ff_free_vlc(&vlc);
122  return 0;
123 }
124 
125 static int decode_frame(AVCodecContext *avctx,
126  void *data, int *data_size,
127  AVPacket *avpkt)
128 {
129  const uint8_t *buf = avpkt->data;
130  int buf_size = avpkt->size;
131  FrapsContext * const s = avctx->priv_data;
132  AVFrame *frame = data;
133  AVFrame * const f = (AVFrame*)&s->frame;
134  uint32_t header;
135  unsigned int version,header_size;
136  unsigned int x, y;
137  const uint32_t *buf32;
138  uint32_t *luma1,*luma2,*cb,*cr;
139  uint32_t offs[4];
140  int i, j, is_chroma, planes;
141  enum PixelFormat pix_fmt;
142  int prev_pic_bit, expected_size;
143 
144  if (buf_size < 4) {
145  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
146  return AVERROR_INVALIDDATA;
147  }
148 
149  header = AV_RL32(buf);
150  version = header & 0xff;
151  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
152  prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
153 
154  if (version > 5) {
155  av_log(avctx, AV_LOG_ERROR,
156  "This file is encoded with Fraps version %d. " \
157  "This codec can only decode versions <= 5.\n", version);
158  return -1;
159  }
160 
161  buf+=4;
162  if (header_size == 8)
163  buf+=4;
164 
165  pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
166  if (avctx->pix_fmt != pix_fmt && f->data[0]) {
167  avctx->release_buffer(avctx, f);
168  }
169  avctx->pix_fmt = pix_fmt;
170 
171  expected_size = header_size;
172 
173  switch (version) {
174  case 0:
175  default:
176  /* Fraps v0 is a reordered YUV420 */
177  if (!prev_pic_bit)
178  expected_size += avctx->width * avctx->height * 3 / 2;
179  if (buf_size != expected_size) {
180  av_log(avctx, AV_LOG_ERROR,
181  "Invalid frame length %d (should be %d)\n",
182  buf_size, expected_size);
183  return AVERROR_INVALIDDATA;
184  }
185 
186  if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
187  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
188  avctx->width, avctx->height);
189  return -1;
190  }
191 
192  f->reference = 1;
196  if (avctx->reget_buffer(avctx, f)) {
197  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
198  return -1;
199  }
200  f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
202 
203  if (f->pict_type == AV_PICTURE_TYPE_I) {
204  buf32=(const uint32_t*)buf;
205  for(y=0; y<avctx->height/2; y++){
206  luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
207  luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
208  cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
209  cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
210  for(x=0; x<avctx->width; x+=8){
211  *(luma1++) = *(buf32++);
212  *(luma1++) = *(buf32++);
213  *(luma2++) = *(buf32++);
214  *(luma2++) = *(buf32++);
215  *(cr++) = *(buf32++);
216  *(cb++) = *(buf32++);
217  }
218  }
219  }
220  break;
221 
222  case 1:
223  /* Fraps v1 is an upside-down BGR24 */
224  if (!prev_pic_bit)
225  expected_size += avctx->width * avctx->height * 3;
226  if (buf_size != expected_size) {
227  av_log(avctx, AV_LOG_ERROR,
228  "Invalid frame length %d (should be %d)\n",
229  buf_size, expected_size);
230  return AVERROR_INVALIDDATA;
231  }
232 
233  f->reference = 1;
237  if (avctx->reget_buffer(avctx, f)) {
238  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
239  return -1;
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)*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  f->reference = 1;
263  if (avctx->reget_buffer(avctx, f)) {
264  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
265  return -1;
266  }
267  /* skip frame */
268  if(buf_size == 8) {
270  f->key_frame = 0;
271  break;
272  }
274  f->key_frame = 1;
275  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
276  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
277  return -1;
278  }
279  for(i = 0; i < planes; i++) {
280  offs[i] = AV_RL32(buf + 4 + i * 4);
281  if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
282  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
283  return -1;
284  }
285  }
286  offs[planes] = buf_size;
287  for(i = 0; i < planes; i++){
288  is_chroma = !!i;
289  s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
290  if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
291  avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
292  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
293  return -1;
294  }
295  }
296  break;
297  case 3:
298  case 5:
299  /* Virtually the same as version 4, but is for RGB24 */
300  planes = 3;
301  f->reference = 1;
305  if (avctx->reget_buffer(avctx, f)) {
306  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
307  return -1;
308  }
309  /* skip frame */
310  if(buf_size == 8) {
312  f->key_frame = 0;
313  break;
314  }
316  f->key_frame = 1;
317  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
318  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
319  return -1;
320  }
321  for(i = 0; i < planes; i++) {
322  offs[i] = AV_RL32(buf + 4 + i * 4);
323  if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
324  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
325  return -1;
326  }
327  }
328  offs[planes] = buf_size;
329  for(i = 0; i < planes; i++){
330  s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
331  if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
332  avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
333  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
334  return -1;
335  }
336  }
337  // convert pseudo-YUV into real RGB
338  for(j = 0; j < avctx->height; j++){
339  for(i = 0; i < avctx->width; i++){
340  f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
341  f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
342  }
343  }
344  break;
345  }
346 
347  *frame = *f;
348  *data_size = sizeof(AVFrame);
349 
350  return buf_size;
351 }
352 
353 
360 {
361  FrapsContext *s = (FrapsContext*)avctx->priv_data;
362 
363  if (s->frame.data[0])
364  avctx->release_buffer(avctx, &s->frame);
365 
366  av_freep(&s->tmpbuf);
367  return 0;
368 }
369 
370 
372  .name = "fraps",
373  .type = AVMEDIA_TYPE_VIDEO,
374  .id = CODEC_ID_FRAPS,
375  .priv_data_size = sizeof(FrapsContext),
376  .init = decode_init,
377  .close = decode_end,
378  .decode = decode_frame,
379  .capabilities = CODEC_CAP_DR1,
380  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
381 };