3 * Copyright (c) 2005 Roine Gustafsson
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
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
36 #define CACHED_BITSTREAM_READER HAVE_FAST_64BIT
37 #define UNCHECKED_BITSTREAM_READER 1
41 #include "bytestream.h"
43 #include "codec_internal.h"
46 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
50 * local variable storage
52 typedef struct FrapsContext {
53 AVCodecContext *avctx;
61 * @param avctx codec context
62 * @return 0 on success or negative if fails
64 static av_cold int decode_init(AVCodecContext *avctx)
66 FrapsContext * const s = avctx->priv_data;
71 ff_bswapdsp_init(&s->bdsp);
77 * Comparator - our nodes should ascend by count
78 * but with preserved symbol order
80 static int huff_cmp(const void *va, const void *vb)
82 const Node *a = va, *b = vb;
83 return (a->count - b->count)*256 + a->sym - b->sym;
87 * decode Fraps v2 packed plane
89 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
90 int h, const uint8_t *src, int size, int Uoff,
98 for (i = 0; i < 256; i++)
99 nodes[i].count = bytestream_get_le32(&src);
101 if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, VLC_BITS,
103 FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
105 /* we have built Huffman table and are ready to decode plane */
107 /* convert bits so they may be used by standard bitreader */
108 s->bdsp.bswap_buf((uint32_t *) s->tmpbuf,
109 (const uint32_t *) src, size >> 2);
111 if ((ret = init_get_bits8(&gb, s->tmpbuf, size)) < 0)
114 for (j = 0; j < h; j++) {
115 for (i = 0; i < w*step; i += step) {
116 dst[i] = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
117 /* lines are stored as deltas between previous lines
118 * and we need to add 0x80 to the first lines of chroma planes
121 dst[i] += dst[i - stride];
124 if (get_bits_left(&gb) < 0) {
126 return AVERROR_INVALIDDATA;
135 static int decode_frame(AVCodecContext *avctx, AVFrame *f,
136 int *got_frame, AVPacket *avpkt)
138 FrapsContext * const s = avctx->priv_data;
139 const uint8_t *buf = avpkt->data;
140 int buf_size = avpkt->size;
142 unsigned int version,header_size;
143 const uint32_t *buf32;
144 uint32_t *luma1,*luma2,*cb,*cr;
146 int i, j, ret, is_chroma;
147 const int planes = 3;
152 av_log(avctx, AV_LOG_ERROR, "Packet is too short\n");
153 return AVERROR_INVALIDDATA;
156 header = AV_RL32(buf);
157 version = header & 0xff;
158 is_pal = buf[1] == 2 && version == 1;
159 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
162 avpriv_report_missing_feature(avctx, "Fraps version %u", version);
163 return AVERROR_PATCHWELCOME;
169 unsigned needed_size = avctx->width * avctx->height + 1024;
170 needed_size += header_size;
171 if (buf_size != needed_size) {
172 av_log(avctx, AV_LOG_ERROR,
173 "Invalid frame length %d (should be %d)\n",
174 buf_size, needed_size);
175 return AVERROR_INVALIDDATA;
177 } else if (version < 2) {
178 unsigned needed_size = avctx->width * avctx->height * 3;
179 if (version == 0) needed_size /= 2;
180 needed_size += header_size;
181 /* bit 31 means same as previous pic */
182 if (header & (1U<<31)) {
186 if (buf_size != needed_size) {
187 av_log(avctx, AV_LOG_ERROR,
188 "Invalid frame length %d (should be %d)\n",
189 buf_size, needed_size);
190 return AVERROR_INVALIDDATA;
198 if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
199 av_log(avctx, AV_LOG_ERROR, "error in data stream\n");
200 return AVERROR_INVALIDDATA;
202 for (i = 0; i < planes; i++) {
203 offs[i] = AV_RL32(buf + 4 + i * 4);
204 if (offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
205 av_log(avctx, AV_LOG_ERROR, "plane %i offset is out of bounds\n", i);
206 return AVERROR_INVALIDDATA;
209 offs[planes] = buf_size - header_size;
210 for (i = 0; i < planes; i++) {
211 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
213 return AVERROR(ENOMEM);
217 f->pict_type = AV_PICTURE_TYPE_I;
218 f->flags |= AV_FRAME_FLAG_KEY;
220 avctx->pix_fmt = version & 1 ? is_pal ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
221 avctx->color_range = version & 1 ? AVCOL_RANGE_UNSPECIFIED
223 avctx->colorspace = version & 1 ? AVCOL_SPC_UNSPECIFIED : AVCOL_SPC_BT709;
225 if ((ret = ff_thread_get_buffer(avctx, f, 0)) < 0)
231 /* Fraps v0 is a reordered YUV420 */
232 if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
233 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
234 avctx->width, avctx->height);
235 return AVERROR_INVALIDDATA;
238 buf32 = (const uint32_t*)buf;
239 for (ptrdiff_t y = 0; y < avctx->height / 2; y++) {
240 luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0] ];
241 luma2 = (uint32_t*)&f->data[0][ (y * 2 + 1) * f->linesize[0] ];
242 cr = (uint32_t*)&f->data[1][ y * f->linesize[1] ];
243 cb = (uint32_t*)&f->data[2][ y * f->linesize[2] ];
244 for (ptrdiff_t x = 0; x < avctx->width; x += 8) {
257 uint32_t *pal = (uint32_t *)f->data[1];
259 for (unsigned y = 0; y < 256; y++) {
260 pal[y] = AV_RL32(buf) | 0xFF000000;
264 for (ptrdiff_t y = 0; y < avctx->height; y++)
265 memcpy(&f->data[0][y * f->linesize[0]],
266 &buf[y * avctx->width],
269 /* Fraps v1 is an upside-down BGR24 */
270 for (ptrdiff_t y = 0; y < avctx->height; y++)
271 memcpy(&f->data[0][(avctx->height - y - 1) * f->linesize[0]],
272 &buf[y * avctx->width * 3],
280 * Fraps v2 is Huffman-coded YUV420 planes
281 * Fraps v4 is virtually the same
283 for (i = 0; i < planes; i++) {
285 if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
286 avctx->width >> is_chroma,
287 avctx->height >> is_chroma,
288 buf + offs[i], offs[i + 1] - offs[i],
289 is_chroma, 1)) < 0) {
290 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
297 /* Virtually the same as version 4, but is for RGB24 */
298 for (i = 0; i < planes; i++) {
299 if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
300 -f->linesize[0], avctx->width, avctx->height,
301 buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
302 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
307 // convert pseudo-YUV into real RGB
308 for (j = 0; j < avctx->height; j++) {
309 uint8_t *line_end = out + 3*avctx->width;
310 while (out < line_end) {
315 out += f->linesize[0] - 3*avctx->width;
327 * @param avctx codec context
328 * @return 0 on success or negative if fails
330 static av_cold int decode_end(AVCodecContext *avctx)
332 FrapsContext *s = avctx->priv_data;
334 av_freep(&s->tmpbuf);
338 const FFCodec ff_fraps_decoder = {
340 CODEC_LONG_NAME("Fraps"),
341 .p.type = AVMEDIA_TYPE_VIDEO,
342 .p.id = AV_CODEC_ID_FRAPS,
343 .priv_data_size = sizeof(FrapsContext),
346 FF_CODEC_DECODE_CB(decode_frame),
347 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,