00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
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;
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 avcodec_get_frame_defaults(&s->frame);
00096 s->frame.data[0] = NULL;
00097
00098 return 0;
00099 }
00100
00101
00102 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
00103 int *data_size, AVPacket *avpkt)
00104 {
00105 const uint8_t *buf = avpkt->data;
00106 int buf_size = avpkt->size;
00107 FlashSVContext *s = avctx->priv_data;
00108 int h_blocks, v_blocks, h_part, v_part, i, j;
00109 GetBitContext gb;
00110
00111
00112 if (buf_size == 0)
00113 return 0;
00114 if (buf_size < 4)
00115 return -1;
00116
00117 init_get_bits(&gb, buf, buf_size * 8);
00118
00119
00120 s->block_width = 16 * (get_bits(&gb, 4) + 1);
00121 s->image_width = get_bits(&gb, 12);
00122 s->block_height = 16 * (get_bits(&gb, 4) + 1);
00123 s->image_height = get_bits(&gb, 12);
00124
00125
00126 h_blocks = s->image_width / s->block_width;
00127 h_part = s->image_width % s->block_width;
00128 v_blocks = s->image_height / s->block_height;
00129 v_part = s->image_height % s->block_height;
00130
00131
00132
00133 if (s->block_size < s->block_width * s->block_height) {
00134 av_free(s->tmpblock);
00135 if ((s->tmpblock = av_malloc(3 * s->block_width * s->block_height)) == NULL) {
00136 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00137 return AVERROR(ENOMEM);
00138 }
00139 }
00140 s->block_size = s->block_width * s->block_height;
00141
00142
00143 if ((avctx->width == 0) && (avctx->height == 0)) {
00144 avctx->width = s->image_width;
00145 avctx->height = s->image_height;
00146 }
00147
00148
00149 if ((avctx->width != s->image_width) || (avctx->height != s->image_height)) {
00150 av_log(avctx, AV_LOG_ERROR, "Frame width or height differs from first frames!\n");
00151 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n", avctx->height,
00152 avctx->width, s->image_height, s->image_width);
00153 return -1;
00154 }
00155
00156 av_log(avctx, AV_LOG_DEBUG, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
00157 s->image_width, s->image_height, s->block_width, s->block_height,
00158 h_blocks, v_blocks, h_part, v_part);
00159
00160 s->frame.reference = 1;
00161 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00162 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
00163 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00164 return -1;
00165 }
00166
00167
00168 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
00169
00170 int hp = j * s->block_height;
00171 int hs = (j < v_blocks) ? s->block_height : v_part;
00172
00173
00174
00175 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
00176 int wp = i * s->block_width;
00177 int ws = (i < h_blocks) ? s->block_width : h_part;
00178
00179
00180 int size = get_bits(&gb, 16);
00181 if (8 * size > get_bits_left(&gb)) {
00182 avctx->release_buffer(avctx, &s->frame);
00183 s->frame.data[0] = NULL;
00184 return -1;
00185 }
00186
00187 if (size == 0) {
00188
00189 } else {
00190
00191 int ret = inflateReset(&(s->zstream));
00192 if (ret != Z_OK) {
00193 av_log(avctx, AV_LOG_ERROR, "error in decompression (reset) of block %dx%d\n", i, j);
00194
00195 }
00196 s->zstream.next_in = buf + (get_bits_count(&gb) / 8);
00197 s->zstream.avail_in = size;
00198 s->zstream.next_out = s->tmpblock;
00199 s->zstream.avail_out = s->block_size * 3;
00200 ret = inflate(&(s->zstream), Z_FINISH);
00201 if (ret == Z_DATA_ERROR) {
00202 av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
00203 inflateSync(&(s->zstream));
00204 ret = inflate(&(s->zstream), Z_FINISH);
00205 }
00206
00207 if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
00208 av_log(avctx, AV_LOG_ERROR, "error in decompression of block %dx%d: %d\n", i, j, ret);
00209
00210 }
00211 copy_region(s->tmpblock, s->frame.data[0], s->image_height - (hp + hs + 1),
00212 wp, hs, ws, s->frame.linesize[0]);
00213 skip_bits_long(&gb, 8 * size);
00214 }
00215 }
00216 }
00217
00218 *data_size = sizeof(AVFrame);
00219 *(AVFrame*)data = s->frame;
00220
00221 if ((get_bits_count(&gb) / 8) != buf_size)
00222 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
00223 buf_size, (get_bits_count(&gb) / 8));
00224
00225
00226 return buf_size;
00227 }
00228
00229
00230 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
00231 {
00232 FlashSVContext *s = avctx->priv_data;
00233 inflateEnd(&(s->zstream));
00234
00235 if (s->frame.data[0])
00236 avctx->release_buffer(avctx, &s->frame);
00237
00238
00239 av_free(s->tmpblock);
00240
00241 return 0;
00242 }
00243
00244
00245 AVCodec ff_flashsv_decoder = {
00246 .name = "flashsv",
00247 .type = AVMEDIA_TYPE_VIDEO,
00248 .id = CODEC_ID_FLASHSV,
00249 .priv_data_size = sizeof(FlashSVContext),
00250 .init = flashsv_decode_init,
00251 .close = flashsv_decode_end,
00252 .decode = flashsv_decode_frame,
00253 .capabilities = CODEC_CAP_DR1,
00254 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
00255 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
00256 };