Libav 0.7.1
libavcodec/fraps.c
Go to the documentation of this file.
00001 /*
00002  * Fraps FPS1 decoder
00003  * Copyright (c) 2005 Roine Gustafsson
00004  * Copyright (c) 2006 Konstantin Shishkov
00005  *
00006  * This file is part of Libav.
00007  *
00008  * Libav is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * Libav is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Libav; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00034 #include "avcodec.h"
00035 #include "get_bits.h"
00036 #include "huffman.h"
00037 #include "bytestream.h"
00038 #include "dsputil.h"
00039 
00040 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
00041 
00045 typedef struct FrapsContext{
00046     AVCodecContext *avctx;
00047     AVFrame frame;
00048     uint8_t *tmpbuf;
00049     DSPContext dsp;
00050 } FrapsContext;
00051 
00052 
00058 static av_cold int decode_init(AVCodecContext *avctx)
00059 {
00060     FrapsContext * const s = avctx->priv_data;
00061 
00062     avctx->coded_frame = (AVFrame*)&s->frame;
00063     avctx->pix_fmt= PIX_FMT_NONE; /* set in decode_frame */
00064 
00065     s->avctx = avctx;
00066     s->tmpbuf = NULL;
00067 
00068     dsputil_init(&s->dsp, avctx);
00069 
00070     return 0;
00071 }
00072 
00077 static int huff_cmp(const void *va, const void *vb){
00078     const Node *a = va, *b = vb;
00079     return (a->count - b->count)*256 + a->sym - b->sym;
00080 }
00081 
00085 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
00086                                int h, const uint8_t *src, int size, int Uoff,
00087                                const int step)
00088 {
00089     int i, j;
00090     GetBitContext gb;
00091     VLC vlc;
00092     Node nodes[512];
00093 
00094     for(i = 0; i < 256; i++)
00095         nodes[i].count = bytestream_get_le32(&src);
00096     size -= 1024;
00097     if (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
00098                            FF_HUFFMAN_FLAG_ZERO_COUNT) < 0)
00099         return -1;
00100     /* we have built Huffman table and are ready to decode plane */
00101 
00102     /* convert bits so they may be used by standard bitreader */
00103     s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
00104 
00105     init_get_bits(&gb, s->tmpbuf, size * 8);
00106     for(j = 0; j < h; j++){
00107         for(i = 0; i < w*step; i += step){
00108             dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
00109             /* lines are stored as deltas between previous lines
00110              * and we need to add 0x80 to the first lines of chroma planes
00111              */
00112             if(j) dst[i] += dst[i - stride];
00113             else if(Uoff) dst[i] += 0x80;
00114         }
00115         dst += stride;
00116     }
00117     free_vlc(&vlc);
00118     return 0;
00119 }
00120 
00121 static int decode_frame(AVCodecContext *avctx,
00122                         void *data, int *data_size,
00123                         AVPacket *avpkt)
00124 {
00125     const uint8_t *buf = avpkt->data;
00126     int buf_size = avpkt->size;
00127     FrapsContext * const s = avctx->priv_data;
00128     AVFrame *frame = data;
00129     AVFrame * const f = (AVFrame*)&s->frame;
00130     uint32_t header;
00131     unsigned int version,header_size;
00132     unsigned int x, y;
00133     const uint32_t *buf32;
00134     uint32_t *luma1,*luma2,*cb,*cr;
00135     uint32_t offs[4];
00136     int i, j, is_chroma, planes;
00137 
00138 
00139     header = AV_RL32(buf);
00140     version = header & 0xff;
00141     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
00142 
00143     if (version > 5) {
00144         av_log(avctx, AV_LOG_ERROR,
00145                "This file is encoded with Fraps version %d. " \
00146                "This codec can only decode versions <= 5.\n", version);
00147         return -1;
00148     }
00149 
00150     buf+=4;
00151     if (header_size == 8)
00152         buf+=4;
00153 
00154     switch(version) {
00155     case 0:
00156     default:
00157         /* Fraps v0 is a reordered YUV420 */
00158         avctx->pix_fmt = PIX_FMT_YUVJ420P;
00159 
00160         if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
00161              (buf_size != header_size) ) {
00162             av_log(avctx, AV_LOG_ERROR,
00163                    "Invalid frame length %d (should be %d)\n",
00164                    buf_size, avctx->width*avctx->height*3/2+header_size);
00165             return -1;
00166         }
00167 
00168         if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
00169             av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
00170                    avctx->width, avctx->height);
00171             return -1;
00172         }
00173 
00174         f->reference = 1;
00175         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00176                           FF_BUFFER_HINTS_PRESERVE |
00177                           FF_BUFFER_HINTS_REUSABLE;
00178         if (avctx->reget_buffer(avctx, f)) {
00179             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00180             return -1;
00181         }
00182         /* bit 31 means same as previous pic */
00183         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00184         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00185 
00186         if (f->pict_type == AV_PICTURE_TYPE_I) {
00187             buf32=(const uint32_t*)buf;
00188             for(y=0; y<avctx->height/2; y++){
00189                 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
00190                 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
00191                 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
00192                 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
00193                 for(x=0; x<avctx->width; x+=8){
00194                     *(luma1++) = *(buf32++);
00195                     *(luma1++) = *(buf32++);
00196                     *(luma2++) = *(buf32++);
00197                     *(luma2++) = *(buf32++);
00198                     *(cr++) = *(buf32++);
00199                     *(cb++) = *(buf32++);
00200                 }
00201             }
00202         }
00203         break;
00204 
00205     case 1:
00206         /* Fraps v1 is an upside-down BGR24 */
00207         avctx->pix_fmt = PIX_FMT_BGR24;
00208 
00209         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
00210              (buf_size != header_size) ) {
00211             av_log(avctx, AV_LOG_ERROR,
00212                    "Invalid frame length %d (should be %d)\n",
00213                    buf_size, avctx->width*avctx->height*3+header_size);
00214             return -1;
00215         }
00216 
00217         f->reference = 1;
00218         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00219                           FF_BUFFER_HINTS_PRESERVE |
00220                           FF_BUFFER_HINTS_REUSABLE;
00221         if (avctx->reget_buffer(avctx, f)) {
00222             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00223             return -1;
00224         }
00225         /* bit 31 means same as previous pic */
00226         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00227         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00228 
00229         if (f->pict_type == AV_PICTURE_TYPE_I) {
00230             for(y=0; y<avctx->height; y++)
00231                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00232                        &buf[y*avctx->width*3],
00233                        3*avctx->width);
00234         }
00235         break;
00236 
00237     case 2:
00238     case 4:
00243         avctx->pix_fmt = PIX_FMT_YUVJ420P;
00244         planes = 3;
00245         f->reference = 1;
00246         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00247                           FF_BUFFER_HINTS_PRESERVE |
00248                           FF_BUFFER_HINTS_REUSABLE;
00249         if (avctx->reget_buffer(avctx, f)) {
00250             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00251             return -1;
00252         }
00253         /* skip frame */
00254         if(buf_size == 8) {
00255             f->pict_type = AV_PICTURE_TYPE_P;
00256             f->key_frame = 0;
00257             break;
00258         }
00259         f->pict_type = AV_PICTURE_TYPE_I;
00260         f->key_frame = 1;
00261         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00262             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00263             return -1;
00264         }
00265         for(i = 0; i < planes; i++) {
00266             offs[i] = AV_RL32(buf + 4 + i * 4);
00267             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00268                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00269                 return -1;
00270             }
00271         }
00272         offs[planes] = buf_size;
00273         for(i = 0; i < planes; i++){
00274             is_chroma = !!i;
00275             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00276             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00277                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00278                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00279                 return -1;
00280             }
00281         }
00282         break;
00283     case 3:
00284     case 5:
00285         /* Virtually the same as version 4, but is for RGB24 */
00286         avctx->pix_fmt = PIX_FMT_BGR24;
00287         planes = 3;
00288         f->reference = 1;
00289         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00290                           FF_BUFFER_HINTS_PRESERVE |
00291                           FF_BUFFER_HINTS_REUSABLE;
00292         if (avctx->reget_buffer(avctx, f)) {
00293             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00294             return -1;
00295         }
00296         /* skip frame */
00297         if(buf_size == 8) {
00298             f->pict_type = AV_PICTURE_TYPE_P;
00299             f->key_frame = 0;
00300             break;
00301         }
00302         f->pict_type = AV_PICTURE_TYPE_I;
00303         f->key_frame = 1;
00304         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00305             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00306             return -1;
00307         }
00308         for(i = 0; i < planes; i++) {
00309             offs[i] = AV_RL32(buf + 4 + i * 4);
00310             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00311                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00312                 return -1;
00313             }
00314         }
00315         offs[planes] = buf_size;
00316         for(i = 0; i < planes; i++){
00317             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00318             if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
00319                     avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
00320                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00321                 return -1;
00322             }
00323         }
00324         // convert pseudo-YUV into real RGB
00325         for(j = 0; j < avctx->height; j++){
00326             for(i = 0; i < avctx->width; i++){
00327                 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00328                 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00329             }
00330         }
00331         break;
00332     }
00333 
00334     *frame = *f;
00335     *data_size = sizeof(AVFrame);
00336 
00337     return buf_size;
00338 }
00339 
00340 
00346 static av_cold int decode_end(AVCodecContext *avctx)
00347 {
00348     FrapsContext *s = (FrapsContext*)avctx->priv_data;
00349 
00350     if (s->frame.data[0])
00351         avctx->release_buffer(avctx, &s->frame);
00352 
00353     av_freep(&s->tmpbuf);
00354     return 0;
00355 }
00356 
00357 
00358 AVCodec ff_fraps_decoder = {
00359     "fraps",
00360     AVMEDIA_TYPE_VIDEO,
00361     CODEC_ID_FRAPS,
00362     sizeof(FrapsContext),
00363     decode_init,
00364     NULL,
00365     decode_end,
00366     decode_frame,
00367     CODEC_CAP_DR1,
00368     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00369 };