• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 FFmpeg.
00007  *
00008  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     int tmpbuf_size;
00050     DSPContext dsp;
00051 } FrapsContext;
00052 
00053 
00059 static av_cold int decode_init(AVCodecContext *avctx)
00060 {
00061     FrapsContext * const s = avctx->priv_data;
00062 
00063     avcodec_get_frame_defaults(&s->frame);
00064     avctx->coded_frame = (AVFrame*)&s->frame;
00065 
00066     s->avctx = avctx;
00067     s->tmpbuf = NULL;
00068 
00069     dsputil_init(&s->dsp, avctx);
00070 
00071     return 0;
00072 }
00073 
00078 static int huff_cmp(const void *va, const void *vb){
00079     const Node *a = va, *b = vb;
00080     return (a->count - b->count)*256 + a->sym - b->sym;
00081 }
00082 
00086 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
00087                                int h, const uint8_t *src, int size, int Uoff,
00088                                const int step)
00089 {
00090     int i, j;
00091     GetBitContext gb;
00092     VLC vlc;
00093     Node nodes[512];
00094 
00095     for(i = 0; i < 256; i++)
00096         nodes[i].count = bytestream_get_le32(&src);
00097     size -= 1024;
00098     if (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
00099                            FF_HUFFMAN_FLAG_ZERO_COUNT) < 0)
00100         return -1;
00101     /* we have built Huffman table and are ready to decode plane */
00102 
00103     /* convert bits so they may be used by standard bitreader */
00104     s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
00105 
00106     init_get_bits(&gb, s->tmpbuf, size * 8);
00107     for(j = 0; j < h; j++){
00108         for(i = 0; i < w*step; i += step){
00109             dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
00110             /* lines are stored as deltas between previous lines
00111              * and we need to add 0x80 to the first lines of chroma planes
00112              */
00113             if(j) dst[i] += dst[i - stride];
00114             else if(Uoff) dst[i] += 0x80;
00115         }
00116         dst += stride;
00117     }
00118     free_vlc(&vlc);
00119     return 0;
00120 }
00121 
00122 static int decode_frame(AVCodecContext *avctx,
00123                         void *data, int *data_size,
00124                         AVPacket *avpkt)
00125 {
00126     const uint8_t *buf = avpkt->data;
00127     int buf_size = avpkt->size;
00128     FrapsContext * const s = avctx->priv_data;
00129     AVFrame *frame = data;
00130     AVFrame * const f = (AVFrame*)&s->frame;
00131     uint32_t header;
00132     unsigned int version,header_size;
00133     unsigned int x, y;
00134     const uint32_t *buf32;
00135     uint32_t *luma1,*luma2,*cb,*cr;
00136     uint32_t offs[4];
00137     int i, j, is_chroma, planes;
00138     enum PixelFormat pix_fmt;
00139 
00140     header = AV_RL32(buf);
00141     version = header & 0xff;
00142     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
00143 
00144     if (version > 5) {
00145         av_log(avctx, AV_LOG_ERROR,
00146                "This file is encoded with Fraps version %d. " \
00147                "This codec can only decode versions <= 5.\n", version);
00148         return -1;
00149     }
00150 
00151     buf+=4;
00152     if (header_size == 8)
00153         buf+=4;
00154 
00155     pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
00156     if (avctx->pix_fmt != pix_fmt && f->data[0]) {
00157         avctx->release_buffer(avctx, f);
00158     }
00159     avctx->pix_fmt = pix_fmt;
00160 
00161     switch(version) {
00162     case 0:
00163     default:
00164         /* Fraps v0 is a reordered YUV420 */
00165         if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
00166              (buf_size != header_size) ) {
00167             av_log(avctx, AV_LOG_ERROR,
00168                    "Invalid frame length %d (should be %d)\n",
00169                    buf_size, avctx->width*avctx->height*3/2+header_size);
00170             return -1;
00171         }
00172 
00173         if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
00174             av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
00175                    avctx->width, avctx->height);
00176             return -1;
00177         }
00178 
00179         f->reference = 1;
00180         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00181                           FF_BUFFER_HINTS_PRESERVE |
00182                           FF_BUFFER_HINTS_REUSABLE;
00183         if (avctx->reget_buffer(avctx, f)) {
00184             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00185             return -1;
00186         }
00187         /* bit 31 means same as previous pic */
00188         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00189         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00190 
00191         if (f->pict_type == AV_PICTURE_TYPE_I) {
00192             buf32=(const uint32_t*)buf;
00193             for(y=0; y<avctx->height/2; y++){
00194                 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
00195                 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
00196                 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
00197                 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
00198                 for(x=0; x<avctx->width; x+=8){
00199                     *(luma1++) = *(buf32++);
00200                     *(luma1++) = *(buf32++);
00201                     *(luma2++) = *(buf32++);
00202                     *(luma2++) = *(buf32++);
00203                     *(cr++) = *(buf32++);
00204                     *(cb++) = *(buf32++);
00205                 }
00206             }
00207         }
00208         break;
00209 
00210     case 1:
00211         /* Fraps v1 is an upside-down BGR24 */
00212         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
00213              (buf_size != header_size) ) {
00214             av_log(avctx, AV_LOG_ERROR,
00215                    "Invalid frame length %d (should be %d)\n",
00216                    buf_size, avctx->width*avctx->height*3+header_size);
00217             return -1;
00218         }
00219 
00220         f->reference = 1;
00221         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00222                           FF_BUFFER_HINTS_PRESERVE |
00223                           FF_BUFFER_HINTS_REUSABLE;
00224         if (avctx->reget_buffer(avctx, f)) {
00225             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00226             return -1;
00227         }
00228         /* bit 31 means same as previous pic */
00229         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00230         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00231 
00232         if (f->pict_type == AV_PICTURE_TYPE_I) {
00233             for(y=0; y<avctx->height; y++)
00234                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00235                        &buf[y*avctx->width*3],
00236                        3*avctx->width);
00237         }
00238         break;
00239 
00240     case 2:
00241     case 4:
00246         planes = 3;
00247         f->reference = 1;
00248         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00249                           FF_BUFFER_HINTS_PRESERVE |
00250                           FF_BUFFER_HINTS_REUSABLE;
00251         if (avctx->reget_buffer(avctx, f)) {
00252             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00253             return -1;
00254         }
00255         /* skip frame */
00256         if(buf_size == 8) {
00257             f->pict_type = AV_PICTURE_TYPE_P;
00258             f->key_frame = 0;
00259             break;
00260         }
00261         f->pict_type = AV_PICTURE_TYPE_I;
00262         f->key_frame = 1;
00263         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00264             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00265             return -1;
00266         }
00267         for(i = 0; i < planes; i++) {
00268             offs[i] = AV_RL32(buf + 4 + i * 4);
00269             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00270                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00271                 return -1;
00272             }
00273         }
00274         offs[planes] = buf_size;
00275         for(i = 0; i < planes; i++){
00276             is_chroma = !!i;
00277             av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00278             if (!s->tmpbuf)
00279                 return AVERROR(ENOMEM);
00280             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00281                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00282                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00283                 return -1;
00284             }
00285         }
00286         break;
00287     case 3:
00288     case 5:
00289         /* Virtually the same as version 4, but is for RGB24 */
00290         planes = 3;
00291         f->reference = 1;
00292         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00293                           FF_BUFFER_HINTS_PRESERVE |
00294                           FF_BUFFER_HINTS_REUSABLE;
00295         if (avctx->reget_buffer(avctx, f)) {
00296             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00297             return -1;
00298         }
00299         /* skip frame */
00300         if(buf_size == 8) {
00301             f->pict_type = AV_PICTURE_TYPE_P;
00302             f->key_frame = 0;
00303             break;
00304         }
00305         f->pict_type = AV_PICTURE_TYPE_I;
00306         f->key_frame = 1;
00307         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00308             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00309             return -1;
00310         }
00311         for(i = 0; i < planes; i++) {
00312             offs[i] = AV_RL32(buf + 4 + i * 4);
00313             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00314                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00315                 return -1;
00316             }
00317         }
00318         offs[planes] = buf_size;
00319         for(i = 0; i < planes; i++){
00320             av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00321             if (!s->tmpbuf)
00322                 return AVERROR(ENOMEM);
00323             if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
00324                     avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
00325                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00326                 return -1;
00327             }
00328         }
00329         // convert pseudo-YUV into real RGB
00330         for(j = 0; j < avctx->height; j++){
00331             for(i = 0; i < avctx->width; i++){
00332                 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00333                 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00334             }
00335         }
00336         break;
00337     }
00338 
00339     *frame = *f;
00340     *data_size = sizeof(AVFrame);
00341 
00342     return buf_size;
00343 }
00344 
00345 
00351 static av_cold int decode_end(AVCodecContext *avctx)
00352 {
00353     FrapsContext *s = (FrapsContext*)avctx->priv_data;
00354 
00355     if (s->frame.data[0])
00356         avctx->release_buffer(avctx, &s->frame);
00357 
00358     av_freep(&s->tmpbuf);
00359     return 0;
00360 }
00361 
00362 
00363 AVCodec ff_fraps_decoder = {
00364     "fraps",
00365     AVMEDIA_TYPE_VIDEO,
00366     CODEC_ID_FRAPS,
00367     sizeof(FrapsContext),
00368     decode_init,
00369     NULL,
00370     decode_end,
00371     decode_frame,
00372     CODEC_CAP_DR1,
00373     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00374 };

Generated on Fri Feb 22 2013 07:24:26 for FFmpeg by  doxygen 1.7.1