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 "bswapdsp.h"
39 #include "internal.h"
40 #include "thread.h"
41 
42 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
43 #define VLC_BITS 11
44 
45 /**
46  * local variable storage
47  */
48 typedef struct FrapsContext {
53 } FrapsContext;
54 
55 
56 /**
57  * initializes decoder
58  * @param avctx codec context
59  * @return 0 on success or negative if fails
60  */
62 {
63  FrapsContext * const s = avctx->priv_data;
64 
65  s->avctx = avctx;
66  s->tmpbuf = NULL;
67 
69 
70  return 0;
71 }
72 
73 /**
74  * Comparator - our nodes should ascend by count
75  * but with preserved symbol order
76  */
77 static int huff_cmp(const void *va, const void *vb)
78 {
79  const Node *a = va, *b = vb;
80  return (a->count - b->count)*256 + a->sym - b->sym;
81 }
82 
83 /**
84  * decode Fraps v2 packed plane
85  */
86 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
87  int h, const uint8_t *src, int size, int Uoff,
88  const int step)
89 {
90  int i, j, ret;
91  GetBitContext gb;
92  VLC vlc;
93  Node nodes[512];
94 
95  for (i = 0; i < 256; i++)
96  nodes[i].count = bytestream_get_le32(&src);
97  size -= 1024;
98  if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
99  nodes, huff_cmp,
101  return ret;
102  /* we have built Huffman table and are ready to decode plane */
103 
104  /* convert bits so they may be used by standard bitreader */
105  s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
106  (const uint32_t *) src, size >> 2);
107 
108  init_get_bits(&gb, s->tmpbuf, size * 8);
109  for (j = 0; j < h; j++) {
110  for (i = 0; i < w*step; i += step) {
111  dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
112  /* lines are stored as deltas between previous lines
113  * and we need to add 0x80 to the first lines of chroma planes
114  */
115  if (j)
116  dst[i] += dst[i - stride];
117  else if (Uoff)
118  dst[i] += 0x80;
119  if (get_bits_left(&gb) < 0) {
120  ff_free_vlc(&vlc);
121  return AVERROR_INVALIDDATA;
122  }
123  }
124  dst += stride;
125  }
126  ff_free_vlc(&vlc);
127  return 0;
128 }
129 
130 static int decode_frame(AVCodecContext *avctx,
131  void *data, int *got_frame,
132  AVPacket *avpkt)
133 {
134  FrapsContext * const s = avctx->priv_data;
135  const uint8_t *buf = avpkt->data;
136  int buf_size = avpkt->size;
137  ThreadFrame frame = { .f = data };
138  AVFrame * const f = data;
139  uint32_t header;
140  unsigned int version,header_size;
141  unsigned int x, y;
142  const uint32_t *buf32;
143  uint32_t *luma1,*luma2,*cb,*cr;
144  uint32_t offs[4];
145  int i, j, ret, is_chroma;
146  const int planes = 3;
147  uint8_t *out;
148 
149  if (buf_size < 4) {
150  av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
151  return AVERROR_INVALIDDATA;
152  }
153 
154  header = AV_RL32(buf);
155  version = header & 0xff;
156  header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
157 
158  if (version > 5) {
159  av_log(avctx, AV_LOG_ERROR,
160  "This file is encoded with Fraps version %d. " \
161  "This codec can only decode versions <= 5.\n", version);
162  return AVERROR_PATCHWELCOME;
163  }
164 
165  buf += header_size;
166 
167  if (version < 2) {
168  unsigned needed_size = avctx->width * avctx->height * 3;
169  if (version == 0) needed_size /= 2;
170  needed_size += header_size;
171  /* bit 31 means same as previous pic */
172  if (header & (1U<<31)) {
173  *got_frame = 0;
174  return buf_size;
175  }
176  if (buf_size != needed_size) {
177  av_log(avctx, AV_LOG_ERROR,
178  "Invalid frame length %d (should be %d)\n",
179  buf_size, needed_size);
180  return AVERROR_INVALIDDATA;
181  }
182  } else {
183  /* skip frame */
184  if (buf_size == 8) {
185  *got_frame = 0;
186  return buf_size;
187  }
188  if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
189  av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
190  return AVERROR_INVALIDDATA;
191  }
192  for (i = 0; i < planes; i++) {
193  offs[i] = AV_RL32(buf + 4 + i * 4);
194  if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
195  av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
196  return AVERROR_INVALIDDATA;
197  }
198  }
199  offs[planes] = buf_size - header_size;
200  for (i = 0; i < planes; i++) {
201  av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
202  if (!s->tmpbuf)
203  return AVERROR(ENOMEM);
204  }
205  }
206 
208  f->key_frame = 1;
209 
210  avctx->pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
211  avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
213  avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
214 
215  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
216  return ret;
217 
218  switch (version) {
219  case 0:
220  default:
221  /* Fraps v0 is a reordered YUV420 */
222  if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
223  av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
224  avctx->width, avctx->height);
225  return AVERROR_INVALIDDATA;
226  }
227 
228  buf32 = (const uint32_t*)buf;
229  for (y = 0; y < avctx->height / 2; y++) {
230  luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
231  luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
232  cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
233  cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
234  for (x = 0; x < avctx->width; x += 8) {
235  *luma1++ = *buf32++;
236  *luma1++ = *buf32++;
237  *luma2++ = *buf32++;
238  *luma2++ = *buf32++;
239  *cr++ = *buf32++;
240  *cb++ = *buf32++;
241  }
242  }
243  break;
244 
245  case 1:
246  /* Fraps v1 is an upside-down BGR24 */
247  for (y = 0; y<avctx->height; y++)
248  memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
249  &buf[y * avctx->width * 3],
250  3 * avctx->width);
251  break;
252 
253  case 2:
254  case 4:
255  /**
256  * Fraps v2 is Huffman-coded YUV420 planes
257  * Fraps v4 is virtually the same
258  */
259  for (i = 0; i < planes; i++) {
260  is_chroma = !!i;
261  if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
262  avctx->width >> is_chroma,
263  avctx->height >> is_chroma,
264  buf + offs[i], offs[i + 1] - offs[i],
265  is_chroma, 1)) < 0) {
266  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
267  return ret;
268  }
269  }
270  break;
271  case 3:
272  case 5:
273  /* Virtually the same as version 4, but is for RGB24 */
274  for (i = 0; i < planes; i++) {
275  if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
276  -f->linesize[0], avctx->width, avctx->height,
277  buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
278  av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
279  return ret;
280  }
281  }
282  out = f->data[0];
283  // convert pseudo-YUV into real RGB
284  for (j = 0; j < avctx->height; j++) {
285  uint8_t *line_end = out + 3*avctx->width;
286  while (out < line_end) {
287  out[0] += out[1];
288  out[2] += out[1];
289  out += 3;
290  }
291  out += f->linesize[0] - 3*avctx->width;
292  }
293  break;
294  }
295 
296  *got_frame = 1;
297 
298  return buf_size;
299 }
300 
301 
302 /**
303  * closes decoder
304  * @param avctx codec context
305  * @return 0 on success or negative if fails
306  */
308 {
309  FrapsContext *s = (FrapsContext*)avctx->priv_data;
310 
311  av_freep(&s->tmpbuf);
312  return 0;
313 }
314 
315 
317  .name = "fraps",
318  .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
319  .type = AVMEDIA_TYPE_VIDEO,
320  .id = AV_CODEC_ID_FRAPS,
321  .priv_data_size = sizeof(FrapsContext),
322  .init = decode_init,
323  .close = decode_end,
324  .decode = decode_frame,
325  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
326 };
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: bswapdsp.h:25
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:502
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
BswapDSPContext bdsp
Definition: fraps.c:50
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int ff_huff_build_tree(AVCodecContext *avctx, VLC *vlc, int nb_codes, int nb_bits, Node *nodes, HuffCmp cmp, int flags)
nodes size must be 2*nb_codes first nb_codes nodes.count must be set
Definition: huffman.c:154
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1960
int size
Definition: avcodec.h:1163
const char * b
Definition: vf_curves.c:109
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:139
int version
Definition: avisynth_c.h:629
AVCodec.
Definition: avcodec.h:3181
Definition: huffman.h:32
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:96
uint8_t
#define av_cold
Definition: attributes.h:74
Multithreading support functions.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
static const uint8_t header[24]
Definition: sdr2.c:67
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:86
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:588
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define FF_HUFFMAN_FLAG_ZERO_COUNT
Definition: huffman.h:39
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
GLsizei count
Definition: opengl_enc.c:109
Libavcodec external API header.
Definition: get_bits.h:63
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:77
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
float y
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
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:555
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
#define VLC_BITS
Definition: fraps.c:43
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:523
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: fraps.c:130
AVCodecContext * avctx
Definition: fraps.c:49
AVS_Value src
Definition: avisynth_c.h:482
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
static av_cold int decode_end(AVCodecContext *avctx)
closes decoder
Definition: fraps.c:307
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1241
static av_cold int decode_init(AVCodecContext *avctx)
initializes decoder
Definition: fraps.c:61
void * buf
Definition: avisynth_c.h:553
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1953
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:410
huffman tree builder and VLC generator
int16_t sym
Definition: huffman.h:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:866
AVCodec ff_fraps_decoder
Definition: fraps.c:316
uint8_t * tmpbuf
Definition: fraps.c:51
void * priv_data
Definition: avcodec.h:1283
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
local variable storage
Definition: fraps.c:48
uint32_t count
Definition: huffman.h:35
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define av_freep(p)
#define stride
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:97
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:85
This structure stores compressed data.
Definition: avcodec.h:1139
int tmpbuf_size
Definition: fraps.c:52
void ff_free_vlc(VLC *vlc)
Definition: bitstream.c:359
for(j=16;j >0;--j)
#define FPS_TAG
Definition: fraps.c:42