Libav 0.7.1
libavcodec/flashsv.c
Go to the documentation of this file.
00001 /*
00002  * Flash Screen Video decoder
00003  * Copyright (C) 2004 Alex Beregszaszi
00004  * Copyright (C) 2006 Benjamin Larsson
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 
00030 /* Bitstream description
00031  * The picture is divided into blocks that are zlib compressed.
00032  *
00033  * The decoder is fed complete frames, the frameheader contains:
00034  * 4bits of block width
00035  * 12bits of frame width
00036  * 4bits of block height
00037  * 12bits of frame height
00038  *
00039  * Directly after the header are the compressed blocks. The blocks
00040  * have their compressed size represented with 16bits in the beginnig.
00041  * If the size = 0 then the block is unchanged from the previous frame.
00042  * All blocks are decompressed until the buffer is consumed.
00043  *
00044  * Encoding ideas, a basic encoder would just use a fixed block size.
00045  * Block sizes can be multipels of 16, from 16 to 256. The blocks don't
00046  * have to be quadratic. A brute force search with a set of diffrent
00047  * block sizes should give a better result then to just use a fixed size.
00048  */
00049 
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 #include <zlib.h>
00053 
00054 #include "avcodec.h"
00055 #include "get_bits.h"
00056 
00057 typedef struct FlashSVContext {
00058     AVCodecContext *avctx;
00059     AVFrame         frame;
00060     int             image_width, image_height;
00061     int             block_width, block_height;
00062     uint8_t        *tmpblock;
00063     int             block_size;
00064     z_stream        zstream;
00065 } FlashSVContext;
00066 
00067 
00068 static void copy_region(uint8_t *sptr, uint8_t *dptr,
00069                         int dx, int dy, int h, int w, int stride)
00070 {
00071     int i;
00072 
00073     for (i = dx + h; i > dx; i--) {
00074         memcpy(dptr + (i * stride) + dy * 3, sptr, w * 3);
00075         sptr += w * 3;
00076     }
00077 }
00078 
00079 
00080 static av_cold int flashsv_decode_init(AVCodecContext *avctx)
00081 {
00082     FlashSVContext *s = avctx->priv_data;
00083     int zret; // Zlib return code
00084 
00085     s->avctx          = avctx;
00086     s->zstream.zalloc = Z_NULL;
00087     s->zstream.zfree  = Z_NULL;
00088     s->zstream.opaque = Z_NULL;
00089     zret = inflateInit(&(s->zstream));
00090     if (zret != Z_OK) {
00091         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00092         return 1;
00093     }
00094     avctx->pix_fmt = PIX_FMT_BGR24;
00095     s->frame.data[0] = NULL;
00096 
00097     return 0;
00098 }
00099 
00100 
00101 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
00102                                 int *data_size, AVPacket *avpkt)
00103 {
00104     const uint8_t *buf = avpkt->data;
00105     int buf_size       = avpkt->size;
00106     FlashSVContext *s  = avctx->priv_data;
00107     int h_blocks, v_blocks, h_part, v_part, i, j;
00108     GetBitContext gb;
00109 
00110     /* no supplementary picture */
00111     if (buf_size == 0)
00112         return 0;
00113     if (buf_size < 4)
00114         return -1;
00115 
00116     init_get_bits(&gb, buf, buf_size * 8);
00117 
00118     /* start to parse the bitstream */
00119     s->block_width  = 16 * (get_bits(&gb,  4) + 1);
00120     s->image_width  =       get_bits(&gb, 12);
00121     s->block_height = 16 * (get_bits(&gb,  4) + 1);
00122     s->image_height =       get_bits(&gb, 12);
00123 
00124     /* calculate amount of blocks and the size of the border blocks */
00125     h_blocks = s->image_width  / s->block_width;
00126     h_part   = s->image_width  % s->block_width;
00127     v_blocks = s->image_height / s->block_height;
00128     v_part   = s->image_height % s->block_height;
00129 
00130     /* the block size could change between frames, make sure the buffer
00131      * is large enough, if not, get a larger one */
00132     if (s->block_size < s->block_width * s->block_height) {
00133         av_free(s->tmpblock);
00134         if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
00135             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00136             return AVERROR(ENOMEM);
00137         }
00138     }
00139     s->block_size = s->block_width * s->block_height;
00140 
00141     /* init the image size once */
00142     if ((avctx->width == 0) && (avctx->height == 0)) {
00143         avctx->width  = s->image_width;
00144         avctx->height = s->image_height;
00145     }
00146 
00147     /* check for changes of image width and image height */
00148     if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
00149         av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
00150         av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d  vs  ch = %d, cv = %d\n", avctx->height,
00151         avctx->width, s->image_height, s->image_width);
00152         return -1;
00153     }
00154 
00155     av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
00156            s->image_width, s->image_height, s->block_width, s->block_height,
00157            h_blocks, v_blocks, h_part, v_part);
00158 
00159     s->frame.reference    = 1;
00160     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00161     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
00162         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00163         return -1;
00164     }
00165 
00166     /* loop over all block columns */
00167     for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
00168 
00169         int hp = j * s->block_height; // horiz position in frame
00170         int hs = (j < v_blocks) ? s->block_height : v_part; // size of block
00171 
00172 
00173         /* loop over all block rows */
00174         for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
00175             int wp = i * s->block_width; // vert position in frame
00176             int ws = (i < h_blocks) ? s->block_width : h_part; // size of block
00177 
00178             /* get the size of the compressed zlib chunk */
00179             int size = get_bits(&gb, 16);
00180             if (8 * size > get_bits_left(&gb)) {
00181                 avctx->release_buffer(avctx, &s->frame);
00182                 s->frame.data[0] = NULL;
00183                 return -1;
00184             }
00185 
00186             if (size == 0) {
00187                 /* no change, don't do anything */
00188             } else {
00189                 /* decompress block */
00190                 int ret = inflateReset(&(s->zstream));
00191                 if (ret != Z_OK) {
00192                     av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
00193                     /* return -1; */
00194                 }
00195                 s->zstream.next_in   = buf + (get_bits_count(&gb) / 8);
00196                 s->zstream.avail_in  = size;
00197                 s->zstream.next_out  = s->tmpblock;
00198                 s->zstream.avail_out = s->block_size * 3;
00199                 ret = inflate(&(s->zstream), Z_FINISH);
00200                 if (ret == Z_DATA_ERROR) {
00201                     av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
00202                     inflateSync(&(s->zstream));
00203                     ret = inflate(&(s->zstream), Z_FINISH);
00204                 }
00205 
00206                 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
00207                     av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
00208                     /* return -1; */
00209                 }
00210                 copy_region(s->tmpblock, s->frame.data[0], s->image_height - (hp + hs + 1),
00211                             wp, hs, ws, s->frame.linesize[0]);
00212                 skip_bits_long(&gb, 8 * size);   /* skip the consumed bits */
00213             }
00214         }
00215     }
00216 
00217     *data_size = sizeof(AVFrame);
00218     *(AVFrame*)data = s->frame;
00219 
00220     if ((get_bits_count(&gb) / 8) != buf_size)
00221         av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
00222                buf_size, (get_bits_count(&gb) / 8));
00223 
00224     /* report that the buffer was completely consumed */
00225     return buf_size;
00226 }
00227 
00228 
00229 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
00230 {
00231     FlashSVContext *s = avctx->priv_data;
00232     inflateEnd(&(s->zstream));
00233     /* release the frame if needed */
00234     if (s->frame.data[0])
00235         avctx->release_buffer(avctx, &s->frame);
00236 
00237     /* free the tmpblock */
00238     av_free(s->tmpblock);
00239 
00240     return 0;
00241 }
00242 
00243 
00244 AVCodec ff_flashsv_decoder = {
00245     .name           = "flashsv",
00246     .type           = AVMEDIA_TYPE_VIDEO,
00247     .id             = CODEC_ID_FLASHSV,
00248     .priv_data_size = sizeof(FlashSVContext),
00249     .init           = flashsv_decode_init,
00250     .close          = flashsv_decode_end,
00251     .decode         = flashsv_decode_frame,
00252     .capabilities   = CODEC_CAP_DR1,
00253     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
00254     .long_name      = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
00255 };