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 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;
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
00101
00102
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
00110
00111
00112 if(j) dst[i] += dst[i - stride];
00113 else if(Uoff) dst[i] += 0x80;
00114 if (get_bits_left(&gb) < 0) {
00115 free_vlc(&vlc);
00116 return AVERROR_INVALIDDATA;
00117 }
00118 }
00119 dst += stride;
00120 }
00121 free_vlc(&vlc);
00122 return 0;
00123 }
00124
00125 static int decode_frame(AVCodecContext *avctx,
00126 void *data, int *data_size,
00127 AVPacket *avpkt)
00128 {
00129 const uint8_t *buf = avpkt->data;
00130 int buf_size = avpkt->size;
00131 FrapsContext * const s = avctx->priv_data;
00132 AVFrame *frame = data;
00133 AVFrame * const f = (AVFrame*)&s->frame;
00134 uint32_t header;
00135 unsigned int version,header_size;
00136 unsigned int x, y;
00137 const uint32_t *buf32;
00138 uint32_t *luma1,*luma2,*cb,*cr;
00139 uint32_t offs[4];
00140 int i, j, is_chroma, planes;
00141 enum PixelFormat pix_fmt;
00142
00143 header = AV_RL32(buf);
00144 version = header & 0xff;
00145 header_size = (header & (1<<30))? 8 : 4;
00146
00147 if (version > 5) {
00148 av_log(avctx, AV_LOG_ERROR,
00149 "This file is encoded with Fraps version %d. " \
00150 "This codec can only decode versions <= 5.\n", version);
00151 return -1;
00152 }
00153
00154 buf+=4;
00155 if (header_size == 8)
00156 buf+=4;
00157
00158 pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P;
00159 if (avctx->pix_fmt != pix_fmt && f->data[0]) {
00160 avctx->release_buffer(avctx, f);
00161 }
00162 avctx->pix_fmt = pix_fmt;
00163
00164 switch(version) {
00165 case 0:
00166 default:
00167
00168 if ( (buf_size != avctx->width*avctx->height*3/2+header_size) &&
00169 (buf_size != header_size) ) {
00170 av_log(avctx, AV_LOG_ERROR,
00171 "Invalid frame length %d (should be %d)\n",
00172 buf_size, avctx->width*avctx->height*3/2+header_size);
00173 return -1;
00174 }
00175
00176 if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) {
00177 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n",
00178 avctx->width, avctx->height);
00179 return -1;
00180 }
00181
00182 f->reference = 1;
00183 f->buffer_hints = FF_BUFFER_HINTS_VALID |
00184 FF_BUFFER_HINTS_PRESERVE |
00185 FF_BUFFER_HINTS_REUSABLE;
00186 if (avctx->reget_buffer(avctx, f)) {
00187 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00188 return -1;
00189 }
00190
00191 f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00192 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00193
00194 if (f->pict_type == AV_PICTURE_TYPE_I) {
00195 buf32=(const uint32_t*)buf;
00196 for(y=0; y<avctx->height/2; y++){
00197 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ];
00198 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ];
00199 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ];
00200 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ];
00201 for(x=0; x<avctx->width; x+=8){
00202 *(luma1++) = *(buf32++);
00203 *(luma1++) = *(buf32++);
00204 *(luma2++) = *(buf32++);
00205 *(luma2++) = *(buf32++);
00206 *(cr++) = *(buf32++);
00207 *(cb++) = *(buf32++);
00208 }
00209 }
00210 }
00211 break;
00212
00213 case 1:
00214
00215 if ( (buf_size != avctx->width*avctx->height*3+header_size) &&
00216 (buf_size != header_size) ) {
00217 av_log(avctx, AV_LOG_ERROR,
00218 "Invalid frame length %d (should be %d)\n",
00219 buf_size, avctx->width*avctx->height*3+header_size);
00220 return -1;
00221 }
00222
00223 f->reference = 1;
00224 f->buffer_hints = FF_BUFFER_HINTS_VALID |
00225 FF_BUFFER_HINTS_PRESERVE |
00226 FF_BUFFER_HINTS_REUSABLE;
00227 if (avctx->reget_buffer(avctx, f)) {
00228 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00229 return -1;
00230 }
00231
00232 f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
00233 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I;
00234
00235 if (f->pict_type == AV_PICTURE_TYPE_I) {
00236 for(y=0; y<avctx->height; y++)
00237 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ],
00238 &buf[y*avctx->width*3],
00239 3*avctx->width);
00240 }
00241 break;
00242
00243 case 2:
00244 case 4:
00249 planes = 3;
00250 f->reference = 1;
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 s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00281 if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma,
00282 avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) {
00283 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00284 return -1;
00285 }
00286 }
00287 break;
00288 case 3:
00289 case 5:
00290
00291 planes = 3;
00292 f->reference = 1;
00293 f->buffer_hints = FF_BUFFER_HINTS_VALID |
00294 FF_BUFFER_HINTS_PRESERVE |
00295 FF_BUFFER_HINTS_REUSABLE;
00296 if (avctx->reget_buffer(avctx, f)) {
00297 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00298 return -1;
00299 }
00300
00301 if(buf_size == 8) {
00302 f->pict_type = AV_PICTURE_TYPE_P;
00303 f->key_frame = 0;
00304 break;
00305 }
00306 f->pict_type = AV_PICTURE_TYPE_I;
00307 f->key_frame = 1;
00308 if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) {
00309 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n");
00310 return -1;
00311 }
00312 for(i = 0; i < planes; i++) {
00313 offs[i] = AV_RL32(buf + 4 + i * 4);
00314 if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) {
00315 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i);
00316 return -1;
00317 }
00318 }
00319 offs[planes] = buf_size;
00320 for(i = 0; i < planes; i++){
00321 s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE);
00322 if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0],
00323 avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) {
00324 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i);
00325 return -1;
00326 }
00327 }
00328
00329 for(j = 0; j < avctx->height; j++){
00330 for(i = 0; i < avctx->width; i++){
00331 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00332 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]];
00333 }
00334 }
00335 break;
00336 }
00337
00338 *frame = *f;
00339 *data_size = sizeof(AVFrame);
00340
00341 return buf_size;
00342 }
00343
00344
00350 static av_cold int decode_end(AVCodecContext *avctx)
00351 {
00352 FrapsContext *s = (FrapsContext*)avctx->priv_data;
00353
00354 if (s->frame.data[0])
00355 avctx->release_buffer(avctx, &s->frame);
00356
00357 av_freep(&s->tmpbuf);
00358 return 0;
00359 }
00360
00361
00362 AVCodec ff_fraps_decoder = {
00363 .name = "fraps",
00364 .type = AVMEDIA_TYPE_VIDEO,
00365 .id = CODEC_ID_FRAPS,
00366 .priv_data_size = sizeof(FrapsContext),
00367 .init = decode_init,
00368 .close = decode_end,
00369 .decode = decode_frame,
00370 .capabilities = CODEC_CAP_DR1,
00371 .long_name = NULL_IF_CONFIG_SMALL("Fraps"),
00372 };