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"
40 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
43 * local variable storage
45 typedef struct FrapsContext {
46 AVCodecContext *avctx;
56 * @param avctx codec context
57 * @return 0 on success or negative if fails
59 static av_cold int decode_init(AVCodecContext *avctx)
61 FrapsContext * const s = avctx->priv_data;
63 avctx->coded_frame = &s->frame;
64 avctx->pix_fmt = AV_PIX_FMT_NONE; /* set in decode_frame */
69 ff_dsputil_init(&s->dsp, avctx);
75 * Comparator - our nodes should ascend by count
76 * but with preserved symbol order
78 static int huff_cmp(const void *va, const void *vb)
80 const Node *a = va, *b = vb;
81 return (a->count - b->count)*256 + a->sym - b->sym;
85 * decode Fraps v2 packed plane
87 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
88 int h, const uint8_t *src, int size, int Uoff,
96 for (i = 0; i < 256; i++)
97 nodes[i].count = bytestream_get_le32(&src);
99 if ((ret = ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
100 FF_HUFFMAN_FLAG_ZERO_COUNT)) < 0)
102 /* we have built Huffman table and are ready to decode plane */
104 /* convert bits so they may be used by standard bitreader */
105 s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
107 init_get_bits(&gb, s->tmpbuf, size * 8);
108 for (j = 0; j < h; j++) {
109 for (i = 0; i < w*step; i += step) {
110 dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
111 /* lines are stored as deltas between previous lines
112 * and we need to add 0x80 to the first lines of chroma planes
115 dst[i] += dst[i - stride];
118 if (get_bits_left(&gb) < 0) {
120 return AVERROR_INVALIDDATA;
129 static int decode_frame(AVCodecContext *avctx,
130 void *data, int *got_frame,
133 FrapsContext * const s = avctx->priv_data;
134 const uint8_t *buf = avpkt->data;
135 int buf_size = avpkt->size;
136 AVFrame *frame = data;
137 AVFrame * const f = &s->frame;
139 unsigned int version,header_size;
141 const uint32_t *buf32;
142 uint32_t *luma1,*luma2,*cb,*cr;
144 int i, j, ret, is_chroma, planes;
145 enum AVPixelFormat pix_fmt;
147 header = AV_RL32(buf);
148 version = header & 0xff;
149 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
152 av_log(avctx, AV_LOG_ERROR,
153 "This file is encoded with Fraps version %d. " \
154 "This codec can only decode versions <= 5.\n", version);
155 return AVERROR_PATCHWELCOME;
159 if (header_size == 8)
162 pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
163 if (avctx->pix_fmt != pix_fmt && f->data[0]) {
164 avctx->release_buffer(avctx, f);
166 avctx->pix_fmt = pix_fmt;
171 /* Fraps v0 is a reordered YUV420 */
172 if ((buf_size != avctx->width * avctx->height * 3 / 2 + header_size) &&
173 (buf_size != header_size)) {
174 av_log(avctx, AV_LOG_ERROR,
175 "Invalid frame length %d (should be %d)\n",
177 avctx->width * avctx->height * 3 / 2 + header_size);
178 return AVERROR_INVALIDDATA;
181 if (((avctx->width % 8) != 0) || ((avctx->height % 2) != 0)) {
182 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
183 avctx->width, avctx->height);
184 return AVERROR_INVALIDDATA;
188 f->buffer_hints = FF_BUFFER_HINTS_VALID |
189 FF_BUFFER_HINTS_PRESERVE |
190 FF_BUFFER_HINTS_REUSABLE;
191 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
192 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
195 /* bit 31 means same as previous pic */
196 f->pict_type = (header & (1U << 31)) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
197 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
199 if (f->pict_type == AV_PICTURE_TYPE_I) {
200 buf32 = (const uint32_t*)buf;
201 for (y = 0; y < avctx->height / 2; y++) {
202 luma1 = (uint32_t*)&f->data[0][ y * 2 * f->linesize[0]];
203 luma2 = (uint32_t*)&f->data[0][(y * 2 + 1) * f->linesize[0]];
204 cr = (uint32_t*)&f->data[1][ y * f->linesize[1]];
205 cb = (uint32_t*)&f->data[2][ y * f->linesize[2]];
206 for (x = 0; x < avctx->width; x += 8) {
207 *(luma1++) = *(buf32++);
208 *(luma1++) = *(buf32++);
209 *(luma2++) = *(buf32++);
210 *(luma2++) = *(buf32++);
211 *(cr++) = *(buf32++);
212 *(cb++) = *(buf32++);
219 /* Fraps v1 is an upside-down BGR24 */
220 if ((buf_size != avctx->width * avctx->height * 3 + header_size) &&
221 (buf_size != header_size) ) {
222 av_log(avctx, AV_LOG_ERROR,
223 "Invalid frame length %d (should be %d)\n",
224 buf_size, avctx->width * avctx->height * 3 + header_size);
225 return AVERROR_INVALIDDATA;
229 f->buffer_hints = FF_BUFFER_HINTS_VALID |
230 FF_BUFFER_HINTS_PRESERVE |
231 FF_BUFFER_HINTS_REUSABLE;
232 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
233 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
236 /* bit 31 means same as previous pic */
237 f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
238 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
240 if (f->pict_type == AV_PICTURE_TYPE_I) {
241 for (y = 0; y<avctx->height; y++)
242 memcpy(&f->data[0][(avctx->height - y) * f->linesize[0]],
243 &buf[y * avctx->width * 3],
251 * Fraps v2 is Huffman-coded YUV420 planes
252 * Fraps v4 is virtually the same
256 f->buffer_hints = FF_BUFFER_HINTS_VALID |
257 FF_BUFFER_HINTS_PRESERVE |
258 FF_BUFFER_HINTS_REUSABLE;
259 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
260 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
265 f->pict_type = AV_PICTURE_TYPE_P;
269 f->pict_type = AV_PICTURE_TYPE_I;
271 if ((AV_RL32(buf) != FPS_TAG) || (buf_size < (planes * 1024 + 24))) {
272 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
273 return AVERROR_INVALIDDATA;
275 for (i = 0; i < planes; i++) {
276 offs[i] = AV_RL32(buf + 4 + i * 4);
277 if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
278 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
279 return AVERROR_INVALIDDATA;
282 offs[planes] = buf_size;
283 for (i = 0; i < planes; i++) {
285 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
286 offs[i + 1] - offs[i] - 1024);
288 return AVERROR(ENOMEM);
289 if ((ret = fraps2_decode_plane(s, f->data[i], f->linesize[i],
290 avctx->width >> is_chroma,
291 avctx->height >> is_chroma,
292 buf + offs[i], offs[i + 1] - offs[i],
293 is_chroma, 1)) < 0) {
294 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
301 /* Virtually the same as version 4, but is for RGB24 */
304 f->buffer_hints = FF_BUFFER_HINTS_VALID |
305 FF_BUFFER_HINTS_PRESERVE |
306 FF_BUFFER_HINTS_REUSABLE;
307 if ((ret = avctx->reget_buffer(avctx, f)) < 0) {
308 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
313 f->pict_type = AV_PICTURE_TYPE_P;
317 f->pict_type = AV_PICTURE_TYPE_I;
319 if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
320 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
321 return AVERROR_INVALIDDATA;
323 for (i = 0; i < planes; i++) {
324 offs[i] = AV_RL32(buf + 4 + i * 4);
325 if (offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
326 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
327 return AVERROR_INVALIDDATA;
330 offs[planes] = buf_size;
331 for (i = 0; i < planes; i++) {
332 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size,
333 offs[i + 1] - offs[i] - 1024);
335 return AVERROR(ENOMEM);
336 if ((ret = fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)),
337 -f->linesize[0], avctx->width, avctx->height,
338 buf + offs[i], offs[i + 1] - offs[i], 0, 3)) < 0) {
339 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
343 // convert pseudo-YUV into real RGB
344 for (j = 0; j < avctx->height; j++) {
345 for (i = 0; i < avctx->width; i++) {
346 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
347 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
362 * @param avctx codec context
363 * @return 0 on success or negative if fails
365 static av_cold int decode_end(AVCodecContext *avctx)
367 FrapsContext *s = (FrapsContext*)avctx->priv_data;
369 if (s->frame.data[0])
370 avctx->release_buffer(avctx, &s->frame);
372 av_freep(&s->tmpbuf);
377 AVCodec ff_fraps_decoder = {
379 .type = AVMEDIA_TYPE_VIDEO,
380 .id = AV_CODEC_ID_FRAPS,
381 .priv_data_size = sizeof(FrapsContext),
384 .decode = decode_frame,
385 .capabilities = CODEC_CAP_DR1,
386 .long_name = NULL_IF_CONFIG_SMALL("Fraps"),