libavcodec/fraps.c
b81f8949
 /*
  * Fraps FPS1 decoder
  * Copyright (c) 2005 Roine Gustafsson
08a4c4bf
  * Copyright (c) 2006 Konstantin Shishkov
b81f8949
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
b81f8949
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
b81f8949
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
b81f8949
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
b81f8949
  */
115329f1
 
b81f8949
 /**
ba87f080
  * @file
b81f8949
  * Lossless Fraps 'FPS1' decoder
0baf34d8
  * @author Roine Gustafsson (roine at users sf net)
08a4c4bf
  * @author Konstantin Shishkov
115329f1
  *
b81f8949
  * Codec algorithm for version 0 is taken from Transcode <www.transcoding.org>
  *
08a4c4bf
  * Version 2 files support by Konstantin Shishkov
b81f8949
  */
115329f1
 
b81f8949
 #include "avcodec.h"
9106a698
 #include "get_bits.h"
437c2079
 #include "huffman.h"
 #include "bytestream.h"
08a4c4bf
 #include "dsputil.h"
b81f8949
 
4935b1b9
 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
b81f8949
 
 /**
  * local variable storage
  */
 typedef struct FrapsContext{
     AVCodecContext *avctx;
     AVFrame frame;
08a4c4bf
     uint8_t *tmpbuf;
80da2dcf
     int tmpbuf_size;
08a4c4bf
     DSPContext dsp;
b81f8949
 } FrapsContext;
 
 
 /**
  * initializes decoder
  * @param avctx codec context
  * @return 0 on success or negative if fails
  */
98a6fff9
 static av_cold int decode_init(AVCodecContext *avctx)
b81f8949
 {
     FrapsContext * const s = avctx->priv_data;
 
01042d41
     avcodec_get_frame_defaults(&s->frame);
b81f8949
     avctx->coded_frame = (AVFrame*)&s->frame;
 
     s->avctx = avctx;
08a4c4bf
     s->tmpbuf = NULL;
 
     dsputil_init(&s->dsp, avctx);
b81f8949
 
     return 0;
 }
 
08a4c4bf
 /**
  * Comparator - our nodes should ascend by count
  * but with preserved symbol order
  */
859cfdc0
 static int huff_cmp(const void *va, const void *vb){
     const Node *a = va, *b = vb;
08a4c4bf
     return (a->count - b->count)*256 + a->sym - b->sym;
 }
 
 /**
  * decode Fraps v2 packed plane
  */
 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
6a02cb82
                                int h, const uint8_t *src, int size, int Uoff,
                                const int step)
08a4c4bf
 {
     int i, j;
     GetBitContext gb;
     VLC vlc;
437c2079
     Node nodes[512];
08a4c4bf
 
437c2079
     for(i = 0; i < 256; i++)
         nodes[i].count = bytestream_get_le32(&src);
     size -= 1024;
a73cbf97
     if (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
                            FF_HUFFMAN_FLAG_ZERO_COUNT) < 0)
08a4c4bf
         return -1;
     /* we have built Huffman table and are ready to decode plane */
 
     /* convert bits so they may be used by standard bitreader */
721052e9
     s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
08a4c4bf
 
     init_get_bits(&gb, s->tmpbuf, size * 8);
     for(j = 0; j < h; j++){
6a02cb82
         for(i = 0; i < w*step; i += step){
437c2079
             dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
08a4c4bf
             /* lines are stored as deltas between previous lines
              * and we need to add 0x80 to the first lines of chroma planes
              */
             if(j) dst[i] += dst[i - stride];
             else if(Uoff) dst[i] += 0x80;
         }
         dst += stride;
     }
     free_vlc(&vlc);
     return 0;
 }
b81f8949
 
115329f1
 static int decode_frame(AVCodecContext *avctx,
b81f8949
                         void *data, int *data_size,
7a00bbad
                         AVPacket *avpkt)
b81f8949
 {
7a00bbad
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
b81f8949
     FrapsContext * const s = avctx->priv_data;
     AVFrame *frame = data;
     AVFrame * const f = (AVFrame*)&s->frame;
     uint32_t header;
     unsigned int version,header_size;
     unsigned int x, y;
1855abe5
     const uint32_t *buf32;
b81f8949
     uint32_t *luma1,*luma2,*cb,*cr;
08a4c4bf
     uint32_t offs[4];
af176191
     int i, j, is_chroma, planes;
b81f8949
 
 
fead30d4
     header = AV_RL32(buf);
b81f8949
     version = header & 0xff;
     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
 
675f7114
     if (version > 5) {
115329f1
         av_log(avctx, AV_LOG_ERROR,
b81f8949
                "This file is encoded with Fraps version %d. " \
675f7114
                "This codec can only decode versions <= 5.\n", version);
b81f8949
         return -1;
     }
 
     buf+=4;
     if (header_size == 8)
         buf+=4;
115329f1
 
b81f8949
     switch(version) {
     case 0:
     default:
         /* Fraps v0 is a reordered YUV420 */
05931ab7
         avctx->pix_fmt = PIX_FMT_YUVJ420P;
b81f8949
 
115329f1
         if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
b81f8949
              (buf_size != header_size) ) {
             av_log(avctx, AV_LOG_ERROR,
115329f1
                    "Invalid frame length %d (should be %d)\n",
b81f8949
                    buf_size, avctx->width*avctx->height*3/2+header_size);
             return -1;
         }
115329f1
 
b81f8949
         if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
115329f1
             av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
b81f8949
                    avctx->width, avctx->height);
             return -1;
         }
 
115329f1
         f->reference = 1;
         f->buffer_hints = FF_BUFFER_HINTS_VALID |
                           FF_BUFFER_HINTS_PRESERVE |
b81f8949
                           FF_BUFFER_HINTS_REUSABLE;
         if (avctx->reget_buffer(avctx, f)) {
             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
             return -1;
115329f1
         }
b81f8949
         /* bit 31 means same as previous pic */
ce5e49b0
         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
b81f8949
 
ce5e49b0
         if (f->pict_type == AV_PICTURE_TYPE_I) {
1855abe5
             buf32=(const uint32_t*)buf;
b81f8949
             for(y=0; y<avctx->height/2; y++){
                 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
                 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
                 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
                 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
                 for(x=0; x<avctx->width; x+=8){
                     *(luma1++) = *(buf32++);
                     *(luma1++) = *(buf32++);
                     *(luma2++) = *(buf32++);
                     *(luma2++) = *(buf32++);
                     *(cr++) = *(buf32++);
                     *(cb++) = *(buf32++);
                 }
             }
         }
         break;
 
     case 1:
         /* Fraps v1 is an upside-down BGR24 */
         avctx->pix_fmt = PIX_FMT_BGR24;
 
115329f1
         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
b81f8949
              (buf_size != header_size) ) {
115329f1
             av_log(avctx, AV_LOG_ERROR,
b81f8949
                    "Invalid frame length %d (should be %d)\n",
                    buf_size, avctx->width*avctx->height*3+header_size);
             return -1;
         }
 
         f->reference = 1;
         f->buffer_hints = FF_BUFFER_HINTS_VALID |
                           FF_BUFFER_HINTS_PRESERVE |
                           FF_BUFFER_HINTS_REUSABLE;
         if (avctx->reget_buffer(avctx, f)) {
             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
             return -1;
         }
         /* bit 31 means same as previous pic */
ce5e49b0
         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
b81f8949
 
ce5e49b0
         if (f->pict_type == AV_PICTURE_TYPE_I) {
b81f8949
             for(y=0; y<avctx->height; y++)
                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
                        &buf[y*avctx->width*3],
de5922f1
                        3*avctx->width);
b81f8949
         }
         break;
 
     case 2:
08a4c4bf
     case 4:
b81f8949
         /**
08a4c4bf
          * Fraps v2 is Huffman-coded YUV420 planes
75a71b6c
          * Fraps v4 is virtually the same
b81f8949
          */
05931ab7
         avctx->pix_fmt = PIX_FMT_YUVJ420P;
75a71b6c
         planes = 3;
08a4c4bf
         f->reference = 1;
         f->buffer_hints = FF_BUFFER_HINTS_VALID |
                           FF_BUFFER_HINTS_PRESERVE |
                           FF_BUFFER_HINTS_REUSABLE;
         if (avctx->reget_buffer(avctx, f)) {
             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
             return -1;
         }
         /* skip frame */
         if(buf_size == 8) {
ce5e49b0
             f->pict_type = AV_PICTURE_TYPE_P;
08a4c4bf
             f->key_frame = 0;
             break;
         }
ce5e49b0
         f->pict_type = AV_PICTURE_TYPE_I;
08a4c4bf
         f->key_frame = 1;
fead30d4
         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
b81f8949
             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
             return -1;
         }
08a4c4bf
         for(i = 0; i < planes; i++) {
fead30d4
             offs[i] = AV_RL32(buf + 4 + i * 4);
08a4c4bf
             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
                 return -1;
             }
         }
         offs[planes] = buf_size;
         for(i = 0; i < planes; i++){
             is_chroma = !!i;
80da2dcf
             av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
67c1a59a
             if (!s->tmpbuf)
                 return AVERROR(ENOMEM);
08a4c4bf
             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
6a02cb82
                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
08a4c4bf
                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
                 return -1;
             }
         }
b81f8949
         break;
675f7114
     case 3:
8c908d7d
     case 5:
         /* Virtually the same as version 4, but is for RGB24 */
         avctx->pix_fmt = PIX_FMT_BGR24;
         planes = 3;
         f->reference = 1;
         f->buffer_hints = FF_BUFFER_HINTS_VALID |
                           FF_BUFFER_HINTS_PRESERVE |
                           FF_BUFFER_HINTS_REUSABLE;
         if (avctx->reget_buffer(avctx, f)) {
             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
             return -1;
         }
         /* skip frame */
         if(buf_size == 8) {
ce5e49b0
             f->pict_type = AV_PICTURE_TYPE_P;
8c908d7d
             f->key_frame = 0;
             break;
         }
ce5e49b0
         f->pict_type = AV_PICTURE_TYPE_I;
8c908d7d
         f->key_frame = 1;
         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
             return -1;
         }
         for(i = 0; i < planes; i++) {
             offs[i] = AV_RL32(buf + 4 + i * 4);
             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
                 return -1;
             }
         }
         offs[planes] = buf_size;
         for(i = 0; i < planes; i++){
80da2dcf
             av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
67c1a59a
             if (!s->tmpbuf)
                 return AVERROR(ENOMEM);
8c908d7d
             if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
af176191
                     avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
8c908d7d
                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
                 return -1;
             }
         }
af176191
         // convert pseudo-YUV into real RGB
         for(j = 0; j < avctx->height; j++){
             for(i = 0; i < avctx->width; i++){
76c655fb
                 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
                 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
af176191
             }
         }
8c908d7d
         break;
b81f8949
     }
 
     *frame = *f;
     *data_size = sizeof(AVFrame);
 
     return buf_size;
 }
 
 
 /**
  * closes decoder
  * @param avctx codec context
  * @return 0 on success or negative if fails
  */
98a6fff9
 static av_cold int decode_end(AVCodecContext *avctx)
b81f8949
 {
     FrapsContext *s = (FrapsContext*)avctx->priv_data;
 
     if (s->frame.data[0])
         avctx->release_buffer(avctx, &s->frame);
 
08a4c4bf
     av_freep(&s->tmpbuf);
b81f8949
     return 0;
 }
 
 
e7e2df27
 AVCodec ff_fraps_decoder = {
b81f8949
     "fraps",
72415b2a
     AVMEDIA_TYPE_VIDEO,
b81f8949
     CODEC_ID_FRAPS,
     sizeof(FrapsContext),
     decode_init,
     NULL,
     decode_end,
     decode_frame,
     CODEC_CAP_DR1,
fe4bf374
     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
b81f8949
 };