• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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     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 
00130 static int decode_frame(AVCodecContext *avctx,
00131                         void *data, int *data_size,
00132                         AVPacket *avpkt)
00133 {
00134     const uint8_t *buf = avpkt->data;
00135     int buf_size = avpkt->size;
00136     FrapsContext * const s = avctx->priv_data;
00137     AVFrame *frame = data;
00138     AVFrame * const f = (AVFrame*)&s->frame;
00139     uint32_t header;
00140     unsigned int version,header_size;
00141     unsigned int x, y;
00142     const uint32_t *buf32;
00143     uint32_t *luma1,*luma2,*cb,*cr;
00144     uint32_t offs[4];
00145     int i, j, is_chroma, planes;
00146 
00147 
00148     header = AV_RL32(buf);
00149     version = header & 0xff;
00150     header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */
00151 
00152     if (version > 5) {
00153         av_log(avctx, AV_LOG_ERROR,
00154                "This file is encoded with Fraps version %d. " \
00155                "This codec can only decode versions <= 5.\n", version);
00156         return -1;
00157     }
00158 
00159     buf+=4;
00160     if (header_size == 8)
00161         buf+=4;
00162 
00163     switch(version) {
00164     case 0:
00165     default:
00166         /* Fraps v0 is a reordered YUV420 */
00167         avctx->pix_fmt = PIX_FMT_YUV420P;
00168 
00169         if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
00170              (buf_size != header_size) ) {
00171             av_log(avctx, AV_LOG_ERROR,
00172                    "Invalid frame length %d (should be %d)\n",
00173                    buf_size, avctx->width*avctx->height*3/2+header_size);
00174             return -1;
00175         }
00176 
00177         if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
00178             av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
00179                    avctx->width, avctx->height);
00180             return -1;
00181         }
00182 
00183         f->reference = 1;
00184         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00185                           FF_BUFFER_HINTS_PRESERVE |
00186                           FF_BUFFER_HINTS_REUSABLE;
00187         if (avctx->reget_buffer(avctx, f)) {
00188             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00189             return -1;
00190         }
00191         /* bit 31 means same as previous pic */
00192         f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
00193         f->key_frame = f->pict_type == FF_I_TYPE;
00194 
00195         if (f->pict_type == FF_I_TYPE) {
00196             buf32=(const uint32_t*)buf;
00197             for(y=0; y<avctx->height/2; y++){
00198                 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
00199                 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
00200                 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
00201                 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
00202                 for(x=0; x<avctx->width; x+=8){
00203                     *(luma1++) = *(buf32++);
00204                     *(luma1++) = *(buf32++);
00205                     *(luma2++) = *(buf32++);
00206                     *(luma2++) = *(buf32++);
00207                     *(cr++) = *(buf32++);
00208                     *(cb++) = *(buf32++);
00209                 }
00210             }
00211         }
00212         break;
00213 
00214     case 1:
00215         /* Fraps v1 is an upside-down BGR24 */
00216         avctx->pix_fmt = PIX_FMT_BGR24;
00217 
00218         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
00219              (buf_size != header_size) ) {
00220             av_log(avctx, AV_LOG_ERROR,
00221                    "Invalid frame length %d (should be %d)\n",
00222                    buf_size, avctx->width*avctx->height*3+header_size);
00223             return -1;
00224         }
00225 
00226         f->reference = 1;
00227         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00228                           FF_BUFFER_HINTS_PRESERVE |
00229                           FF_BUFFER_HINTS_REUSABLE;
00230         if (avctx->reget_buffer(avctx, f)) {
00231             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00232             return -1;
00233         }
00234         /* bit 31 means same as previous pic */
00235         f->pict_type = (header & (1<<31))? FF_P_TYPE : FF_I_TYPE;
00236         f->key_frame = f->pict_type == FF_I_TYPE;
00237 
00238         if (f->pict_type == FF_I_TYPE) {
00239             for(y=0; y<avctx->height; y++)
00240                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00241                        &buf[y*avctx->width*3],
00242                        3*avctx->width);
00243         }
00244         break;
00245 
00246     case 2:
00247     case 4:
00252         avctx->pix_fmt = PIX_FMT_YUV420P;
00253         planes = 3;
00254         f->reference = 1;
00255         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00256                           FF_BUFFER_HINTS_PRESERVE |
00257                           FF_BUFFER_HINTS_REUSABLE;
00258         if (avctx->reget_buffer(avctx, f)) {
00259             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00260             return -1;
00261         }
00262         /* skip frame */
00263         if(buf_size == 8) {
00264             f->pict_type = FF_P_TYPE;
00265             f->key_frame = 0;
00266             break;
00267         }
00268         f->pict_type = FF_I_TYPE;
00269         f->key_frame = 1;
00270         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00271             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00272             return -1;
00273         }
00274         for(i = 0; i < planes; i++) {
00275             offs[i] = AV_RL32(buf + 4 + i * 4);
00276             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00277                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00278                 return -1;
00279             }
00280         }
00281         offs[planes] = buf_size;
00282         for(i = 0; i < planes; i++){
00283             is_chroma = !!i;
00284             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00285             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00286                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00287                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00288                 return -1;
00289             }
00290         }
00291         break;
00292     case 3:
00293     case 5:
00294         /* Virtually the same as version 4, but is for RGB24 */
00295         avctx->pix_fmt = PIX_FMT_BGR24;
00296         planes = 3;
00297         f->reference = 1;
00298         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00299                           FF_BUFFER_HINTS_PRESERVE |
00300                           FF_BUFFER_HINTS_REUSABLE;
00301         if (avctx->reget_buffer(avctx, f)) {
00302             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00303             return -1;
00304         }
00305         /* skip frame */
00306         if(buf_size == 8) {
00307             f->pict_type = FF_P_TYPE;
00308             f->key_frame = 0;
00309             break;
00310         }
00311         f->pict_type = FF_I_TYPE;
00312         f->key_frame = 1;
00313         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00314             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00315             return -1;
00316         }
00317         for(i = 0; i < planes; i++) {
00318             offs[i] = AV_RL32(buf + 4 + i * 4);
00319             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00320                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00321                 return -1;
00322             }
00323         }
00324         offs[planes] = buf_size;
00325         for(i = 0; i < planes; i++){
00326             s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00327             if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
00328                     avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
00329                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00330                 return -1;
00331             }
00332         }
00333         // convert pseudo-YUV into real RGB
00334         for(j = 0; j < avctx->height; j++){
00335             for(i = 0; i < avctx->width; i++){
00336                 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00337                 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00338             }
00339         }
00340         break;
00341     }
00342 
00343     *frame = *f;
00344     *data_size = sizeof(AVFrame);
00345 
00346     return buf_size;
00347 }
00348 
00349 
00355 static av_cold int decode_end(AVCodecContext *avctx)
00356 {
00357     FrapsContext *s = (FrapsContext*)avctx->priv_data;
00358 
00359     if (s->frame.data[0])
00360         avctx->release_buffer(avctx, &s->frame);
00361 
00362     av_freep(&s->tmpbuf);
00363     return 0;
00364 }
00365 
00366 
00367 AVCodec fraps_decoder = {
00368     "fraps",
00369     AVMEDIA_TYPE_VIDEO,
00370     CODEC_ID_FRAPS,
00371     sizeof(FrapsContext),
00372     decode_init,
00373     NULL,
00374     decode_end,
00375     decode_frame,
00376     CODEC_CAP_DR1,
00377     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00378 };

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1