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 *data_size,
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 PixelFormat pix_fmt;
00146
00147 header = AV_RL32(buf);
00148 version = header & 0xff;
00149 header_size = (header & (1<<30))? 8 : 4;
00150
00151 if (version > 5) {
00152 av_log(avctx, AV_LOG_ERROR,
00153 "This file is encoded with Fraps version %d. " \
00154 "This codec can only decode versions <= 5.\n", version);
00155 return -1;
00156 }
00157
00158 buf += header_size;
00159
00160 if (version < 2) {
00161 unsigned needed_size = avctx->width*avctx->height*3;
00162 if (version == 0) needed_size /= 2;
00163 needed_size += header_size;
00164
00165 if (header & (1U<<31)) {
00166 *data_size = 0;
00167 return buf_size;
00168 }
00169 if (buf_size != needed_size) {
00170 av_log(avctx, AV_LOG_ERROR,
00171 "Invalid frame length %d (should be %d)\n",
00172 buf_size, needed_size);
00173 return -1;
00174 }
00175 } else {
00176
00177 if (buf_size == 8) {
00178 *data_size = 0;
00179 return buf_size;
00180 }
00181 if (AV_RL32(buf) != FPS_TAG || buf_size < planes*1024 + 24) {
00182 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00183 return -1;
00184 }
00185 for(i = 0; i < planes; i++) {
00186 offs[i] = AV_RL32(buf + 4 + i * 4);
00187 if(offs[i] >= buf_size - header_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00188 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00189 return -1;
00190 }
00191 }
00192 offs[planes] = buf_size - header_size;
00193 for(i = 0; i < planes; i++) {
00194 av_fast_padded_malloc(&s->tmpbuf, &s->tmpbuf_size, offs[i + 1] - offs[i] - 1024);
00195 if (!s->tmpbuf)
00196 return AVERROR(ENOMEM);
00197 }
00198 }
00199
00200 if (f->data[0])
00201 ff_thread_release_buffer(avctx, f);
00202 f->pict_type = AV_PICTURE_TYPE_I;
00203 f->key_frame = 1;
00204 f->reference = 0;
00205 f->buffer_hints = FF_BUFFER_HINTS_VALID;
00206
00207 pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
00208 if (avctx->pix_fmt != pix_fmt && f->data[0]) {
00209 avctx->release_buffer(avctx, f);
00210 }
00211 avctx->pix_fmt = pix_fmt;
00212
00213 if (ff_thread_get_buffer(avctx, f)) {
00214 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00215 return -1;
00216 }
00217
00218 switch(version) {
00219 case 0:
00220 default:
00221
00222 if ( (avctx->width % 8) != 0 || (avctx->height % 2) != 0 ) {
00223 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
00224 avctx->width, avctx->height);
00225 return -1;
00226 }
00227
00228 buf32=(const uint32_t*)buf;
00229 for(y=0; y<avctx->height/2; y++){
00230 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
00231 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
00232 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
00233 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
00234 for(x=0; x<avctx->width; x+=8){
00235 *luma1++ = *buf32++;
00236 *luma1++ = *buf32++;
00237 *luma2++ = *buf32++;
00238 *luma2++ = *buf32++;
00239 *cr++ = *buf32++;
00240 *cb++ = *buf32++;
00241 }
00242 }
00243 break;
00244
00245 case 1:
00246
00247 for(y=0; y<avctx->height; y++)
00248 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00249 &buf[y*avctx->width*3],
00250 3*avctx->width);
00251 break;
00252
00253 case 2:
00254 case 4:
00259 for(i = 0; i < planes; i++){
00260 is_chroma = !!i;
00261 if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00262 avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00263 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00264 return -1;
00265 }
00266 }
00267 break;
00268 case 3:
00269 case 5:
00270
00271 for(i = 0; i < planes; i++){
00272 if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
00273 avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
00274 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00275 return -1;
00276 }
00277 }
00278 out = f->data[0];
00279
00280 for(j = 0; j < avctx->height; j++){
00281 uint8_t *line_end = out + 3*avctx->width;
00282 while (out < line_end) {
00283 out[0] += out[1];
00284 out[2] += out[1];
00285 out += 3;
00286 }
00287 out += f->linesize[0] - 3*avctx->width;
00288 }
00289 break;
00290 }
00291
00292 *frame = *f;
00293 *data_size = sizeof(AVFrame);
00294
00295 return buf_size;
00296 }
00297
00298
00304 static av_cold int decode_end(AVCodecContext *avctx)
00305 {
00306 FrapsContext *s = (FrapsContext*)avctx->priv_data;
00307
00308 if (s->frame.data[0])
00309 avctx->release_buffer(avctx, &s->frame);
00310
00311 av_freep(&s->tmpbuf);
00312 return 0;
00313 }
00314
00315
00316 AVCodec ff_fraps_decoder = {
00317 .name = "fraps",
00318 .type = AVMEDIA_TYPE_VIDEO,
00319 .id = AV_CODEC_ID_FRAPS,
00320 .priv_data_size = sizeof(FrapsContext),
00321 .init = decode_init,
00322 .close = decode_end,
00323 .decode = decode_frame,
00324 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
00325 .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00326 };