3 * Copyright (c) 2005 Roine Gustafsson
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of Libav.
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.
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.
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
25 * Lossless Fraps 'FPS1' decoder
26 * @author Roine Gustafsson (roine at users sf net)
27 * @author Konstantin Shishkov
29 * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
31 * Version 2 files support by Konstantin Shishkov
37 #include "bytestream.h"
41 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
45 * local variable storage
47 typedef struct FrapsContext {
48 AVCodecContext *avctx;
58 * @param avctx codec context
59 * @return 0 on success or negative if fails
61 static av_cold int decode_init(AVCodecContext *avctx)
63 FrapsContext * const s = avctx->priv_data;
65 avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
70 s->frame = av_frame_alloc();
72 return AVERROR(ENOMEM);
74 ff_bswapdsp_init(&s->bdsp);
80 * Comparator - our nodes should ascend by count
81 * but with preserved symbol order
83 static int huff_cmp(const void *va, const void *vb)
85 const Node *a = va, *b = vb;
86 return (a->count - b->count)*256 + a->sym - b->sym;
90 * decode Fraps v2 packed plane
92 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
93 int h, const uint8_t *src, int size, int Uoff,
101 for (i = 0; i < 256; i++)
102 nodes[i].count = bytestream_get_le32(&src);
104 if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
106 FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
108 /* we have built Huffman table and are ready to decode plane */
110 /* convert bits so they may be used by standard bitreader */
111 s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
112 (const uint32_t *) src, size >> 2);
114 init_get_bits(&gb, s->tmpbuf, size * 8);
115 for (j = 0; j < h; j++) {
116 for (i = 0; i < w*step; i += step) {
117 dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
118 /* lines are stored as deltas between previous lines
119 * and we need to add 0x80 to the first lines of chroma planes
122 dst[i] += dst[i - stride];
125 if (get_bits_left(&gb) < 0) {
127 return AVERROR_INVALIDDATA;
136 static int decode_frame(AVCodecContext *avctx,
137 void *data, int *got_frame,
140 FrapsContext * const s = avctx->priv_data;
141 const uint8_t *buf = avpkt->data;
142 int buf_size = avpkt->size;
143 AVFrame *frame = data;
144 AVFrame * const f = s->frame;
146 unsigned int version,header_size;
148 const uint32_t *buf32;
149 uint32_t *luma1,*luma2,*cb,*cr;
151 int i, j, ret, is_chroma, planes;
152 enum AVPixelFormat pix_fmt;
153 int prev_pic_bit, expected_size;
156 av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
157 return AVERROR_INVALIDDATA;
160 header = AV_RL32(buf);
161 version = header & 0xff;
162 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
163 prev_pic_bit = header & (1U << 31); /* bit 31 means same as previous pic */
166 av_log(avctx, AV_LOG_ERROR,
167 "This file is encoded with Fraps version %d. " \
168 "This codec can only decode versions <= 5.\n", version);
169 return AVERROR_PATCHWELCOME;
173 if (header_size == 8)
176 pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
177 if (avctx->pix_fmt != pix_fmt && f->data[0]) {
180 avctx->pix_fmt = pix_fmt;
181 avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
184 expected_size = header_size;
189 /* Fraps v0 is a reordered YUV420 */
191 expected_size += avctx->width * avctx->height * 3 / 2;
192 if (buf_size != expected_size) {
193 av_log(avctx, AV_LOG_ERROR,
194 "Invalid frame length %d (should be %d)\n",
195 buf_size, expected_size);
196 return AVERROR_INVALIDDATA;
199 if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
200 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
201 avctx->width, avctx->height);
202 return AVERROR_INVALIDDATA;
205 if ((ret = ff_reget_buffer(avctx, f)) < 0) {
206 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
209 f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
210 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
212 if (f->pict_type == AV_PICTURE_TYPE_I) {
213 buf32 = (const uint32_t*)buf;
214 for (y = 0; y < avctx->height / 2; y++) {
215 luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
216 luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
217 cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
218 cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
219 for (x = 0; x < avctx->width; x += 8) {
220 *(luma1++) = *(buf32++);
221 *(luma1++) = *(buf32++);
222 *(luma2++) = *(buf32++);
223 *(luma2++) = *(buf32++);
224 *(cr++) = *(buf32++);
225 *(cb++) = *(buf32++);
232 /* Fraps v1 is an upside-down BGR24 */
234 expected_size += avctx->width * avctx->height * 3;
235 if (buf_size != expected_size) {
236 av_log(avctx, AV_LOG_ERROR,
237 "Invalid frame length %d (should be %d)\n",
238 buf_size, expected_size);
239 return AVERROR_INVALIDDATA;
242 if ((ret = ff_reget_buffer(avctx, f)) < 0) {
243 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
246 f->pict_type = prev_pic_bit ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
247 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
249 if (f->pict_type == AV_PICTURE_TYPE_I) {
250 for (y = 0; y<avctx->height; y++)
251 memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
252 &buf[y * avctx->width * 3],
260 * Fraps v2 is Huffman-coded YUV420 planes
261 * Fraps v4 is virtually the same
264 if ((ret = ff_reget_buffer(avctx, f)) < 0) {
265 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
270 f->pict_type = AV_PICTURE_TYPE_P;
274 f->pict_type = AV_PICTURE_TYPE_I;
276 if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
277 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
278 return AVERROR_INVALIDDATA;
280 for (i = 0; i < planes; i++) {
281 offs[i] = AV_RL32(buf + 4 + i * 4);
282 if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
283 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
284 return AVERROR_INVALIDDATA;
287 offs[planes] = buf_size;
288 for (i = 0; i < planes; i++) {
290 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
291 offs[i + 1] - offs[i] - 1024);
293 return AVERROR(ENOMEM);
294 if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
295 avctx->width >> is_chroma,
296 avctx->height >> is_chroma,
297 buf + offs[i], offs[i + 1] - offs[i],
298 is_chroma, 1)) < 0) {
299 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
306 /* Virtually the same as version 4, but is for RGB24 */
308 if ((ret = ff_reget_buffer(avctx, f)) < 0) {
309 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
314 f->pict_type = AV_PICTURE_TYPE_P;
318 f->pict_type = AV_PICTURE_TYPE_I;
320 if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
321 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
322 return AVERROR_INVALIDDATA;
324 for (i = 0; i < planes; i++) {
325 offs[i] = AV_RL32(buf + 4 + i * 4);
326 if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
327 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
328 return AVERROR_INVALIDDATA;
331 offs[planes] = buf_size;
332 for (i = 0; i < planes; i++) {
333 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
334 offs[i + 1] - offs[i] - 1024);
336 return AVERROR(ENOMEM);
337 if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
338 -f->linesize[0], avctx->width, avctx->height,
339 buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
340 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
344 // convert pseudo-YUV into real RGB
345 for (j = 0; j < avctx->height; j++) {
346 for (i = 0; i < avctx->width; i++) {
347 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
348 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
354 if ((ret = av_frame_ref(frame, f)) < 0)
364 * @param avctx codec context
365 * @return 0 on success or negative if fails
367 static av_cold int decode_end(AVCodecContext *avctx)
369 FrapsContext *s = (FrapsContext*)avctx->priv_data;
371 av_frame_free(&s->frame);
373 av_freep(&s->tmpbuf);
378 AVCodec ff_fraps_decoder = {
380 .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
381 .type = AVMEDIA_TYPE_VIDEO,
382 .id = AV_CODEC_ID_FRAPS,
383 .priv_data_size = sizeof(FrapsContext),
386 .decode = decode_frame,
387 .capabilities = AV_CODEC_CAP_DR1,