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 
143  header = AV_RL32(buf);
144  version = header & 0xff;
145  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
146 
147  if (version > 5) {
148  av_log(avctx, AV_LOG_ERROR,
149  "This file is encoded with Fraps version %d. " \
150  "This codec can only decode versions <= 5.\n", version);
151  return -1;
152  }
153 
154  buf+=4;
155  if (header_size == 8)
156  buf+=4;
157 
158  pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
159  if (avctx->pix_fmt != pix_fmt && f->data[0]) {
160  avctx->release_buffer(avctx, f);
161  }
162  avctx->pix_fmt = pix_fmt;
163 
164  switch(version) {
165  case 0:
166  default:
167  /* Fraps v0 is a reordered YUV420 */
168  if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
169  (buf_size != header_size) ) {
170  av_log(avctx, AV_LOG_ERROR,
171  "Invalid frame length %d (should be %d)\n",
172  buf_size, avctx->width*avctx->height*3/2+header_size);
173  return -1;
174  }
175 
176  if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
177  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
178  avctx->width, avctx->height);
179  return -1;
180  }
181 
182  f->reference = 1;
186  if (avctx->reget_buffer(avctx, f)) {
187  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
188  return -1;
189  }
190  /* bit 31 means same as previous pic */
191  f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
193 
194  if (f->pict_type == AV_PICTURE_TYPE_I) {
195  buf32=(const uint32_t*)buf;
196  for(y=0; y<avctx->height/2; y++){
197  luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
198  luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
199  cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
200  cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
201  for(x=0; x<avctx->width; x+=8){
202  *(luma1++) = *(buf32++);
203  *(luma1++) = *(buf32++);
204  *(luma2++) = *(buf32++);
205  *(luma2++) = *(buf32++);
206  *(cr++) = *(buf32++);
207  *(cb++) = *(buf32++);
208  }
209  }
210  }
211  break;
212 
213  case 1:
214  /* Fraps v1 is an upside-down BGR24 */
215  if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
216  (buf_size != header_size) ) {
217  av_log(avctx, AV_LOG_ERROR,
218  "Invalid frame length %d (should be %d)\n",
219  buf_size, avctx->width*avctx->height*3+header_size);
220  return -1;
221  }
222 
223  f->reference = 1;
227  if (avctx->reget_buffer(avctx, f)) {
228  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
229  return -1;
230  }
231  /* bit 31 means same as previous pic */
232  f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
234 
235  if (f->pict_type == AV_PICTURE_TYPE_I) {
236  for(y=0; y<avctx->height; y++)
237  memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
238  &buf[y*avctx->width*3],
239  3*avctx->width);
240  }
241  break;
242 
243  case 2:
244  case 4:
249  planes = 3;
250  f->reference = 1;
254  if (avctx->reget_buffer(avctx, f)) {
255  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
256  return -1;
257  }
258  /* skip frame */
259  if(buf_size == 8) {
261  f->key_frame = 0;
262  break;
263  }
265  f->key_frame = 1;
266  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
267  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
268  return -1;
269  }
270  for(i = 0; i < planes; i++) {
271  offs[i] = AV_RL32(buf + 4 + i * 4);
272  if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
273  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
274  return -1;
275  }
276  }
277  offs[planes] = buf_size;
278  for(i = 0; i < planes; i++){
279  is_chroma = !!i;
280  s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
281  if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
282  avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
283  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
284  return -1;
285  }
286  }
287  break;
288  case 3:
289  case 5:
290  /* Virtually the same as version 4, but is for RGB24 */
291  planes = 3;
292  f->reference = 1;
296  if (avctx->reget_buffer(avctx, f)) {
297  av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
298  return -1;
299  }
300  /* skip frame */
301  if(buf_size == 8) {
303  f->key_frame = 0;
304  break;
305  }
307  f->key_frame = 1;
308  if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
309  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
310  return -1;
311  }
312  for(i = 0; i < planes; i++) {
313  offs[i] = AV_RL32(buf + 4 + i * 4);
314  if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
315  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
316  return -1;
317  }
318  }
319  offs[planes] = buf_size;
320  for(i = 0; i < planes; i++){
321  s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
322  if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
323  avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
324  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
325  return -1;
326  }
327  }
328  // convert pseudo-YUV into real RGB
329  for(j = 0; j < avctx->height; j++){
330  for(i = 0; i < avctx->width; i++){
331  f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
332  f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
333  }
334  }
335  break;
336  }
337 
338  *frame = *f;
339  *data_size = sizeof(AVFrame);
340 
341  return buf_size;
342 }
343 
344 
351 {
352  FrapsContext *s = (FrapsContext*)avctx->priv_data;
353 
354  if (s->frame.data[0])
355  avctx->release_buffer(avctx, &s->frame);
356 
357  av_freep(&s->tmpbuf);
358  return 0;
359 }
360 
361 
363  .name = "fraps",
364  .type = AVMEDIA_TYPE_VIDEO,
365  .id = CODEC_ID_FRAPS,
366  .priv_data_size = sizeof(FrapsContext),
367  .init = decode_init,
368  .close = decode_end,
369  .decode = decode_frame,
370  .capabilities = CODEC_CAP_DR1,
371  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
372 };