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 #include "thread.h"
00040
00041 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
00042
00046 typedef struct FrapsContext{
00047 AVCodecContext *avctx;
00048 AVFrame frame;
00049 uint8_t *tmpbuf;
00050 int tmpbuf_size;
00051 DSPContext dsp;
00052 } FrapsContext;
00053
00054
00060 static av_cold int decode_init(AVCodecContext *avctx)
00061 {
00062 FrapsContext * const s = avctx->priv_data;
00063
00064 avcodec_get_frame_defaults(&s->frame);
00065 avctx->coded_frame = &s->frame;
00066
00067 s->avctx = avctx;
00068 s->tmpbuf = NULL;
00069
00070 ff_dsputil_init(&s->dsp, avctx);
00071
00072 return 0;
00073 }
00074
00079 static int huff_cmp(const void *va, const void *vb){
00080 const Node *a = va, *b = vb;
00081 return (a->count - b->count)*256 + a->sym - b->sym;
00082 }
00083
00087 static int fraps2_decode_plane(FrapsContext *s, uint8_t *dst, int stride, int w,
00088 int h, const uint8_t *src, int size, int Uoff,
00089 const int step)
00090 {
00091 int i, j;
00092 GetBitContext gb;
00093 VLC vlc;
00094 Node nodes[512];
00095
00096 for(i = 0; i < 256; i++)
00097 nodes[i].count = bytestream_get_le32(&src);
00098 size -= 1024;
00099 if (ff_huff_build_tree(s->avctx, &vlc, 256, nodes, huff_cmp,
00100 FF_HUFFMAN_FLAG_ZERO_COUNT) < 0)
00101 return -1;
00102
00103
00104
00105 s->dsp.bswap_buf((uint32_t *)s->tmpbuf, (const uint32_t *)src, size >> 2);
00106
00107 init_get_bits(&gb, s->tmpbuf, size * 8);
00108 for(j = 0; j < h; j++){
00109 for(i = 0; i < w*step; i += step){
00110 dst[i] = get_vlc2(&gb, vlc.table, 9, 3);
00111
00112
00113
00114 if(j) dst[i] += dst[i - stride];
00115 else if(Uoff) dst[i] += 0x80;
00116 if (get_bits_left(&gb) < 0) {
00117 ff_free_vlc(&vlc);
00118 return AVERROR_INVALIDDATA;
00119 }
00120 }
00121 dst += stride;
00122 }
00123 ff_free_vlc(&vlc);
00124 return 0;
00125 }
00126
00127 static int decode_frame(AVCodecContext *avctx,
00128 void *data, int *got_frame,
00129 AVPacket *avpkt)
00130 {
00131 const uint8_t *buf = avpkt->data;
00132 int buf_size = avpkt->size;
00133 FrapsContext * const s = avctx->priv_data;
00134 AVFrame *frame = data;
00135 AVFrame * const f = &s->frame;
00136 uint32_t header;
00137 unsigned int version,header_size;
00138 unsigned int x, y;
00139 const uint32_t *buf32;
00140 uint32_t *luma1,*luma2,*cb,*cr;
00141 uint32_t offs[4];
00142 int i, j, is_chroma;
00143 const int planes = 3;
00144 uint8_t *out;
00145 enum AVPixelFormat pix_fmt;
00146 int ret;
00147
00148 header = AV_RL32(buf);
00149 version = header & 0xff;
00150 header_size = (header & (1<<30))? 8 : 4;
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 AVERROR_PATCHWELCOME;
00157 }
00158
00159 buf += header_size;
00160
00161 if (version < 2) {
00162 unsigned needed_size = avctx->width*avctx->height*3;
00163 if (version == 0) needed_size /= 2;
00164 needed_size += header_size;
00165
00166 if (header & (1U<<31)) {
00167 *got_frame = 0;
00168 return buf_size;
00169 }
00170 if (buf_size != needed_size) {
00171 av_log(avctx, AV_LOG_ERROR,
00172 "Invalid frame length %d (should be %d)\n",
00173 buf_size, needed_size);
00174 return AVERROR_INVALIDDATA;
00175 }
00176 } else {
00177
00178 if (buf_size == 8) {
00179 *got_frame = 0;
00180 return buf_size;
00181 }
00182 if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
00183 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00184 return AVERROR_INVALIDDATA;
00185 }
00186 for(i = 0; i < planes; i++) {
00187 offs[i] = AV_RL32(buf + 4 + i * 4);
00188 if(offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00189 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00190 return AVERROR_INVALIDDATA;
00191 }
00192 }
00193 offs[planes] = buf_size - header_size;
00194 for(i = 0; i < planes; i++) {
00195 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
00196 if (!s->tmpbuf)
00197 return AVERROR(ENOMEM);
00198 }
00199 }
00200
00201 if (f->data[0])
00202 ff_thread_release_buffer(avctx, f);
00203 f->pict_type = AV_PICTURE_TYPE_I;
00204 f->key_frame = 1;
00205 f->reference = 0;
00206 f->buffer_hints = FF_BUFFER_HINTS_VALID;
00207
00208 pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
00209 if (avctx->pix_fmt != pix_fmt && f->data[0]) {
00210 avctx->release_buffer(avctx, f);
00211 }
00212 avctx->pix_fmt = pix_fmt;
00213
00214 if ((ret = ff_thread_get_buffer(avctx, f))) {
00215 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00216 return ret;
00217 }
00218
00219 switch(version) {
00220 case 0:
00221 default:
00222
00223 if ( (avctx->width % 8) != 0 || (avctx->height % 2) != 0 ) {
00224 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
00225 avctx->width, avctx->height);
00226 return AVERROR_INVALIDDATA;
00227 }
00228
00229 buf32=(const uint32_t*)buf;
00230 for(y=0; y<avctx->height/2; y++){
00231 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
00232 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
00233 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
00234 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
00235 for(x=0; x<avctx->width; x+=8){
00236 *luma1++ = *buf32++;
00237 *luma1++ = *buf32++;
00238 *luma2++ = *buf32++;
00239 *luma2++ = *buf32++;
00240 *cr++ = *buf32++;
00241 *cb++ = *buf32++;
00242 }
00243 }
00244 break;
00245
00246 case 1:
00247
00248 for(y=0; y<avctx->height; y++)
00249 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00250 &buf[y*avctx->width*3],
00251 3*avctx->width);
00252 break;
00253
00254 case 2:
00255 case 4:
00260 for(i = 0; i < planes; i++){
00261 is_chroma = !!i;
00262 if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00263 avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00264 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00265 return AVERROR_INVALIDDATA;
00266 }
00267 }
00268 break;
00269 case 3:
00270 case 5:
00271
00272 for(i = 0; i < planes; i++){
00273 if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
00274 avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
00275 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00276 return AVERROR_INVALIDDATA;
00277 }
00278 }
00279 out = f->data[0];
00280
00281 for(j = 0; j < avctx->height; j++){
00282 uint8_t *line_end = out + 3*avctx->width;
00283 while (out < line_end) {
00284 out[0] += out[1];
00285 out[2] += out[1];
00286 out += 3;
00287 }
00288 out += f->linesize[0] - 3*avctx->width;
00289 }
00290 break;
00291 }
00292
00293 *frame = *f;
00294 *got_frame = 1;
00295
00296 return buf_size;
00297 }
00298
00299
00305 static av_cold int decode_end(AVCodecContext *avctx)
00306 {
00307 FrapsContext *s = (FrapsContext*)avctx->priv_data;
00308
00309 if (s->frame.data[0])
00310 avctx->release_buffer(avctx, &s->frame);
00311
00312 av_freep(&s->tmpbuf);
00313 return 0;
00314 }
00315
00316
00317 AVCodec ff_fraps_decoder = {
00318 .name = "fraps",
00319 .type = AVMEDIA_TYPE_VIDEO,
00320 .id = AV_CODEC_ID_FRAPS,
00321 .priv_data_size = sizeof(FrapsContext),
00322 .init = decode_init,
00323 .close = decode_end,
00324 .decode = decode_frame,
00325 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00326 .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00327 };