Libav 0.7.1
|
00001 /* 00002 * Fraps FPS1 decoder 00003 * Copyright (c) 2005 Roine Gustafsson 00004 * Copyright (c) 2006 Konstantin Shishkov 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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; /* set in decode_frame */ 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 /* we have built Huffman table and are ready to decode plane */ 00101 00102 /* convert bits so they may be used by standard bitreader */ 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 /* lines are stored as deltas between previous lines 00110 * and we need to add 0x80 to the first lines of chroma planes 00111 */ 00112 if(j) dst[i] += dst[i - stride]; 00113 else if(Uoff) dst[i] += 0x80; 00114 } 00115 dst += stride; 00116 } 00117 free_vlc(&vlc); 00118 return 0; 00119 } 00120 00121 static int decode_frame(AVCodecContext *avctx, 00122 void *data, int *data_size, 00123 AVPacket *avpkt) 00124 { 00125 const uint8_t *buf = avpkt->data; 00126 int buf_size = avpkt->size; 00127 FrapsContext * const s = avctx->priv_data; 00128 AVFrame *frame = data; 00129 AVFrame * const f = (AVFrame*)&s->frame; 00130 uint32_t header; 00131 unsigned int version,header_size; 00132 unsigned int x, y; 00133 const uint32_t *buf32; 00134 uint32_t *luma1,*luma2,*cb,*cr; 00135 uint32_t offs[4]; 00136 int i, j, is_chroma, planes; 00137 enum PixelFormat pix_fmt; 00138 00139 header = AV_RL32(buf); 00140 version = header & 0xff; 00141 header_size = (header & (1<<30))? 8 : 4; /* bit 30 means pad to 8 bytes */ 00142 00143 if (version > 5) { 00144 av_log(avctx, AV_LOG_ERROR, 00145 "This file is encoded with Fraps version %d. " \ 00146 "This codec can only decode versions <= 5.\n", version); 00147 return -1; 00148 } 00149 00150 buf+=4; 00151 if (header_size == 8) 00152 buf+=4; 00153 00154 pix_fmt = version & 1 ? PIX_FMT_BGR24 : PIX_FMT_YUVJ420P; 00155 if (avctx->pix_fmt != pix_fmt && f->data[0]) { 00156 avctx->release_buffer(avctx, f); 00157 } 00158 avctx->pix_fmt = pix_fmt; 00159 00160 switch(version) { 00161 case 0: 00162 default: 00163 /* Fraps v0 is a reordered YUV420 */ 00164 if ( (buf_size != avctx->width*avctx->height*3/2+header_size) && 00165 (buf_size != header_size) ) { 00166 av_log(avctx, AV_LOG_ERROR, 00167 "Invalid frame length %d (should be %d)\n", 00168 buf_size, avctx->width*avctx->height*3/2+header_size); 00169 return -1; 00170 } 00171 00172 if (( (avctx->width % 8) != 0) || ( (avctx->height % 2) != 0 )) { 00173 av_log(avctx, AV_LOG_ERROR, "Invalid frame size %dx%d\n", 00174 avctx->width, avctx->height); 00175 return -1; 00176 } 00177 00178 f->reference = 1; 00179 f->buffer_hints = FF_BUFFER_HINTS_VALID | 00180 FF_BUFFER_HINTS_PRESERVE | 00181 FF_BUFFER_HINTS_REUSABLE; 00182 if (avctx->reget_buffer(avctx, f)) { 00183 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00184 return -1; 00185 } 00186 /* bit 31 means same as previous pic */ 00187 f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I; 00188 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I; 00189 00190 if (f->pict_type == AV_PICTURE_TYPE_I) { 00191 buf32=(const uint32_t*)buf; 00192 for(y=0; y<avctx->height/2; y++){ 00193 luma1=(uint32_t*)&f->data[0][ y*2*f->linesize[0] ]; 00194 luma2=(uint32_t*)&f->data[0][ (y*2+1)*f->linesize[0] ]; 00195 cr=(uint32_t*)&f->data[1][ y*f->linesize[1] ]; 00196 cb=(uint32_t*)&f->data[2][ y*f->linesize[2] ]; 00197 for(x=0; x<avctx->width; x+=8){ 00198 *(luma1++) = *(buf32++); 00199 *(luma1++) = *(buf32++); 00200 *(luma2++) = *(buf32++); 00201 *(luma2++) = *(buf32++); 00202 *(cr++) = *(buf32++); 00203 *(cb++) = *(buf32++); 00204 } 00205 } 00206 } 00207 break; 00208 00209 case 1: 00210 /* Fraps v1 is an upside-down BGR24 */ 00211 if ( (buf_size != avctx->width*avctx->height*3+header_size) && 00212 (buf_size != header_size) ) { 00213 av_log(avctx, AV_LOG_ERROR, 00214 "Invalid frame length %d (should be %d)\n", 00215 buf_size, avctx->width*avctx->height*3+header_size); 00216 return -1; 00217 } 00218 00219 f->reference = 1; 00220 f->buffer_hints = FF_BUFFER_HINTS_VALID | 00221 FF_BUFFER_HINTS_PRESERVE | 00222 FF_BUFFER_HINTS_REUSABLE; 00223 if (avctx->reget_buffer(avctx, f)) { 00224 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00225 return -1; 00226 } 00227 /* bit 31 means same as previous pic */ 00228 f->pict_type = (header & (1U<<31))? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I; 00229 f->key_frame = f->pict_type == AV_PICTURE_TYPE_I; 00230 00231 if (f->pict_type == AV_PICTURE_TYPE_I) { 00232 for(y=0; y<avctx->height; y++) 00233 memcpy(&f->data[0][ (avctx->height-y)*f->linesize[0] ], 00234 &buf[y*avctx->width*3], 00235 3*avctx->width); 00236 } 00237 break; 00238 00239 case 2: 00240 case 4: 00245 planes = 3; 00246 f->reference = 1; 00247 f->buffer_hints = FF_BUFFER_HINTS_VALID | 00248 FF_BUFFER_HINTS_PRESERVE | 00249 FF_BUFFER_HINTS_REUSABLE; 00250 if (avctx->reget_buffer(avctx, f)) { 00251 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00252 return -1; 00253 } 00254 /* skip frame */ 00255 if(buf_size == 8) { 00256 f->pict_type = AV_PICTURE_TYPE_P; 00257 f->key_frame = 0; 00258 break; 00259 } 00260 f->pict_type = AV_PICTURE_TYPE_I; 00261 f->key_frame = 1; 00262 if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) { 00263 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n"); 00264 return -1; 00265 } 00266 for(i = 0; i < planes; i++) { 00267 offs[i] = AV_RL32(buf + 4 + i * 4); 00268 if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) { 00269 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i); 00270 return -1; 00271 } 00272 } 00273 offs[planes] = buf_size; 00274 for(i = 0; i < planes; i++){ 00275 is_chroma = !!i; 00276 s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE); 00277 if(fraps2_decode_plane(s, f->data[i], f->linesize[i], avctx->width >> is_chroma, 00278 avctx->height >> is_chroma, buf + offs[i], offs[i + 1] - offs[i], is_chroma, 1) < 0) { 00279 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); 00280 return -1; 00281 } 00282 } 00283 break; 00284 case 3: 00285 case 5: 00286 /* Virtually the same as version 4, but is for RGB24 */ 00287 planes = 3; 00288 f->reference = 1; 00289 f->buffer_hints = FF_BUFFER_HINTS_VALID | 00290 FF_BUFFER_HINTS_PRESERVE | 00291 FF_BUFFER_HINTS_REUSABLE; 00292 if (avctx->reget_buffer(avctx, f)) { 00293 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00294 return -1; 00295 } 00296 /* skip frame */ 00297 if(buf_size == 8) { 00298 f->pict_type = AV_PICTURE_TYPE_P; 00299 f->key_frame = 0; 00300 break; 00301 } 00302 f->pict_type = AV_PICTURE_TYPE_I; 00303 f->key_frame = 1; 00304 if ((AV_RL32(buf) != FPS_TAG)||(buf_size < (planes*1024 + 24))) { 00305 av_log(avctx, AV_LOG_ERROR, "Fraps: error in data stream\n"); 00306 return -1; 00307 } 00308 for(i = 0; i < planes; i++) { 00309 offs[i] = AV_RL32(buf + 4 + i * 4); 00310 if(offs[i] >= buf_size || (i && offs[i] <= offs[i - 1] + 1024)) { 00311 av_log(avctx, AV_LOG_ERROR, "Fraps: plane %i offset is out of bounds\n", i); 00312 return -1; 00313 } 00314 } 00315 offs[planes] = buf_size; 00316 for(i = 0; i < planes; i++){ 00317 s->tmpbuf = av_realloc(s->tmpbuf, offs[i + 1] - offs[i] - 1024 + FF_INPUT_BUFFER_PADDING_SIZE); 00318 if(fraps2_decode_plane(s, f->data[0] + i + (f->linesize[0] * (avctx->height - 1)), -f->linesize[0], 00319 avctx->width, avctx->height, buf + offs[i], offs[i + 1] - offs[i], 0, 3) < 0) { 00320 av_log(avctx, AV_LOG_ERROR, "Error decoding plane %i\n", i); 00321 return -1; 00322 } 00323 } 00324 // convert pseudo-YUV into real RGB 00325 for(j = 0; j < avctx->height; j++){ 00326 for(i = 0; i < avctx->width; i++){ 00327 f->data[0][0 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]]; 00328 f->data[0][2 + i*3 + j*f->linesize[0]] += f->data[0][1 + i*3 + j*f->linesize[0]]; 00329 } 00330 } 00331 break; 00332 } 00333 00334 *frame = *f; 00335 *data_size = sizeof(AVFrame); 00336 00337 return buf_size; 00338 } 00339 00340 00346 static av_cold int decode_end(AVCodecContext *avctx) 00347 { 00348 FrapsContext *s = (FrapsContext*)avctx->priv_data; 00349 00350 if (s->frame.data[0]) 00351 avctx->release_buffer(avctx, &s->frame); 00352 00353 av_freep(&s->tmpbuf); 00354 return 0; 00355 } 00356 00357 00358 AVCodec ff_fraps_decoder = { 00359 "fraps", 00360 AVMEDIA_TYPE_VIDEO, 00361 CODEC_ID_FRAPS, 00362 sizeof(FrapsContext), 00363 decode_init, 00364 NULL, 00365 decode_end, 00366 decode_frame, 00367 CODEC_CAP_DR1, 00368 .long_name = NULL_IF_CONFIG_SMALL("Fraps"), 00369 };