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