00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
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     
00102 
00103     
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             
00111 
00112 
00113             if(j) dst[i] += dst[i - stride];
00114             else if(Uoff) dst[i] += 0x80;
00115             if (get_bits_left(&gb) < 0) {
00116                 free_vlc(&vlc);
00117                 return AVERROR_INVALIDDATA;
00118             }
00119         }
00120         dst += stride;
00121     }
00122     free_vlc(&vlc);
00123     return 0;
00124 }
00125 
00126 static int decode_frame(AVCodecContext *avctx,
00127                         void *data, int *data_size,
00128                         AVPacket *avpkt)
00129 {
00130     const uint8_t *buf = avpkt->data;
00131     int buf_size = avpkt->size;
00132     FrapsContext * const s = avctx->priv_data;
00133     AVFrame *frame = data;
00134     AVFrame * const f = (AVFrame*)&s->frame;
00135     uint32_t header;
00136     unsigned int version,header_size;
00137     unsigned int x, y;
00138     const uint32_t *buf32;
00139     uint32_t *luma1,*luma2,*cb,*cr;
00140     uint32_t offs[4];
00141     int i, j, is_chroma, planes;
00142 
00143 
00144     header = AV_RL32(buf);
00145     version = header & 0xff;
00146     header_size = (header & (1<<30))? 8 : 4; 
00147 
00148     if (version > 5) {
00149         av_log(avctx, AV_LOG_ERROR,
00150                "This file is encoded with Fraps version %d. " \
00151                "This codec can only decode versions <= 5.\n", version);
00152         return -1;
00153     }
00154 
00155     buf+=4;
00156     if (header_size == 8)
00157         buf+=4;
00158 
00159     switch(version) {
00160     case 0:
00161     default:
00162         
00163         avctx->pix_fmt = PIX_FMT_YUVJ420P;
00164 
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 = 3;
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         
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         
00212         avctx->pix_fmt = PIX_FMT_BGR24;
00213 
00214         if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
00215              (buf_size != header_size) ) {
00216             av_log(avctx, AV_LOG_ERROR,
00217                    "Invalid frame length %d (should be %d)\n",
00218                    buf_size, avctx->width*avctx->height*3+header_size);
00219             return -1;
00220         }
00221 
00222         f->reference = 3;
00223         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00224                           FF_BUFFER_HINTS_PRESERVE |
00225                           FF_BUFFER_HINTS_REUSABLE;
00226         if (avctx->reget_buffer(avctx, f)) {
00227             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00228             return -1;
00229         }
00230         
00231         f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00232         f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00233 
00234         if (f->pict_type == AV_PICTURE_TYPE_I) {
00235             for(y=0; y<avctx->height; y++)
00236                 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00237                        &buf[y*avctx->width*3],
00238                        3*avctx->width);
00239         }
00240         break;
00241 
00242     case 2:
00243     case 4:
00248         avctx->pix_fmt = PIX_FMT_YUVJ420P;
00249         planes = 3;
00250         f->reference = 3;
00251         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00252                           FF_BUFFER_HINTS_PRESERVE |
00253                           FF_BUFFER_HINTS_REUSABLE;
00254         if (avctx->reget_buffer(avctx, f)) {
00255             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00256             return -1;
00257         }
00258         
00259         if(buf_size == 8) {
00260             f->pict_type = AV_PICTURE_TYPE_P;
00261             f->key_frame = 0;
00262             break;
00263         }
00264         f->pict_type = AV_PICTURE_TYPE_I;
00265         f->key_frame = 1;
00266         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00267             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00268             return -1;
00269         }
00270         for(i = 0; i < planes; i++) {
00271             offs[i] = AV_RL32(buf + 4 + i * 4);
00272             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00273                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00274                 return -1;
00275             }
00276         }
00277         offs[planes] = buf_size;
00278         for(i = 0; i < planes; i++){
00279             is_chroma = !!i;
00280             av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00281             if (!s->tmpbuf)
00282                 return AVERROR(ENOMEM);
00283             if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00284                     avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00285                 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00286                 return -1;
00287             }
00288         }
00289         break;
00290     case 3:
00291     case 5:
00292         
00293         avctx->pix_fmt = PIX_FMT_BGR24;
00294         planes = 3;
00295         f->reference = 3;
00296         f->buffer_hints = FF_BUFFER_HINTS_VALID |
00297                           FF_BUFFER_HINTS_PRESERVE |
00298                           FF_BUFFER_HINTS_REUSABLE;
00299         if (avctx->reget_buffer(avctx, f)) {
00300             av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00301             return -1;
00302         }
00303         
00304         if(buf_size == 8) {
00305             f->pict_type = AV_PICTURE_TYPE_P;
00306             f->key_frame = 0;
00307             break;
00308         }
00309         f->pict_type = AV_PICTURE_TYPE_I;
00310         f->key_frame = 1;
00311         if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00312             av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00313             return -1;
00314         }
00315         for(i = 0; i < planes; i++) {
00316             offs[i] = AV_RL32(buf + 4 + i * 4);
00317             if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00318                 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00319                 return -1;
00320             }
00321         }
00322         offs[planes] = buf_size;
00323         for(i = 0; i < planes; i++){
00324             av_fast_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00325             if (!s->tmpbuf)
00326                 return AVERROR(ENOMEM);
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         
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 ff_fraps_decoder = {
00368     .name           = "fraps",
00369     .type           = AVMEDIA_TYPE_VIDEO,
00370     .id             = CODEC_ID_FRAPS,
00371     .priv_data_size = sizeof(FrapsContext),
00372     .init           = decode_init,
00373     .close          = decode_end,
00374     .decode         = decode_frame,
00375     .capabilities   = CODEC_CAP_DR1,
00376     .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00377 };