00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <zlib.h>
00039
00040 #include "libavutil/intreadwrite.h"
00041 #include "avcodec.h"
00042 #include "bytestream.h"
00043 #include "get_bits.h"
00044
00045 typedef struct BlockInfo {
00046 uint8_t *pos;
00047 int size;
00048 } BlockInfo;
00049
00050 typedef struct FlashSVContext {
00051 AVCodecContext *avctx;
00052 AVFrame frame;
00053 int image_width, image_height;
00054 int block_width, block_height;
00055 uint8_t *tmpblock;
00056 int block_size;
00057 z_stream zstream;
00058 int ver;
00059 const uint32_t *pal;
00060 int is_keyframe;
00061 uint8_t *keyframedata;
00062 uint8_t *keyframe;
00063 BlockInfo *blocks;
00064 uint8_t *deflate_block;
00065 int deflate_block_size;
00066 int color_depth;
00067 int zlibprime_curr, zlibprime_prev;
00068 int diff_start, diff_height;
00069 } FlashSVContext;
00070
00071
00072 static int decode_hybrid(const uint8_t *sptr, uint8_t *dptr, int dx, int dy,
00073 int h, int w, int stride, const uint32_t *pal)
00074 {
00075 int x, y;
00076 const uint8_t *orig_src = sptr;
00077
00078 for (y = dx+h; y > dx; y--) {
00079 uint8_t *dst = dptr + (y * stride) + dy * 3;
00080 for (x = 0; x < w; x++) {
00081 if (*sptr & 0x80) {
00082
00083 unsigned c = AV_RB16(sptr) & ~0x8000;
00084 unsigned b = c & 0x1F;
00085 unsigned g = (c >> 5) & 0x1F;
00086 unsigned r = c >> 10;
00087
00088 *dst++ = (b << 3) | (b >> 2);
00089 *dst++ = (g << 3) | (g >> 2);
00090 *dst++ = (r << 3) | (r >> 2);
00091 sptr += 2;
00092 } else {
00093
00094 uint32_t c = pal[*sptr++];
00095 bytestream_put_le24(&dst, c);
00096 }
00097 }
00098 }
00099 return sptr - orig_src;
00100 }
00101
00102 static av_cold int flashsv_decode_init(AVCodecContext *avctx)
00103 {
00104 FlashSVContext *s = avctx->priv_data;
00105 int zret;
00106
00107 s->avctx = avctx;
00108 s->zstream.zalloc = Z_NULL;
00109 s->zstream.zfree = Z_NULL;
00110 s->zstream.opaque = Z_NULL;
00111 zret = inflateInit(&s->zstream);
00112 if (zret != Z_OK) {
00113 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00114 return 1;
00115 }
00116 avctx->pix_fmt = AV_PIX_FMT_BGR24;
00117 avcodec_get_frame_defaults(&s->frame);
00118 s->frame.data[0] = NULL;
00119
00120 return 0;
00121 }
00122
00123
00124 static int flashsv2_prime(FlashSVContext *s, uint8_t *src, int size)
00125 {
00126 z_stream zs;
00127 int zret;
00128
00129 if (!src)
00130 return AVERROR_INVALIDDATA;
00131
00132 zs.zalloc = NULL;
00133 zs.zfree = NULL;
00134 zs.opaque = NULL;
00135
00136 s->zstream.next_in = src;
00137 s->zstream.avail_in = size;
00138 s->zstream.next_out = s->tmpblock;
00139 s->zstream.avail_out = s->block_size * 3;
00140 inflate(&s->zstream, Z_SYNC_FLUSH);
00141
00142 if (deflateInit(&zs, 0) != Z_OK)
00143 return -1;
00144 zs.next_in = s->tmpblock;
00145 zs.avail_in = s->block_size * 3 - s->zstream.avail_out;
00146 zs.next_out = s->deflate_block;
00147 zs.avail_out = s->deflate_block_size;
00148 deflate(&zs, Z_SYNC_FLUSH);
00149 deflateEnd(&zs);
00150
00151 if ((zret = inflateReset(&s->zstream)) != Z_OK) {
00152 av_log(s->avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00153 return AVERROR_UNKNOWN;
00154 }
00155
00156 s->zstream.next_in = s->deflate_block;
00157 s->zstream.avail_in = s->deflate_block_size - zs.avail_out;
00158 s->zstream.next_out = s->tmpblock;
00159 s->zstream.avail_out = s->block_size * 3;
00160 inflate(&s->zstream, Z_SYNC_FLUSH);
00161
00162 return 0;
00163 }
00164
00165 static int flashsv_decode_block(AVCodecContext *avctx, AVPacket *avpkt,
00166 GetBitContext *gb, int block_size,
00167 int width, int height, int x_pos, int y_pos,
00168 int blk_idx)
00169 {
00170 struct FlashSVContext *s = avctx->priv_data;
00171 uint8_t *line = s->tmpblock;
00172 int k;
00173 int ret = inflateReset(&s->zstream);
00174 if (ret != Z_OK) {
00175 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
00176 return AVERROR_UNKNOWN;
00177 }
00178 if (s->zlibprime_curr || s->zlibprime_prev) {
00179 ret = flashsv2_prime(s,
00180 s->blocks[blk_idx].pos,
00181 s->blocks[blk_idx].size);
00182 if (ret < 0)
00183 return ret;
00184 }
00185 s->zstream.next_in = avpkt->data + get_bits_count(gb) / 8;
00186 s->zstream.avail_in = block_size;
00187 s->zstream.next_out = s->tmpblock;
00188 s->zstream.avail_out = s->block_size * 3;
00189 ret = inflate(&s->zstream, Z_FINISH);
00190 if (ret == Z_DATA_ERROR) {
00191 av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
00192 inflateSync(&s->zstream);
00193 ret = inflate(&s->zstream, Z_FINISH);
00194 }
00195
00196 if (ret != Z_OK && ret != Z_STREAM_END) {
00197
00198 }
00199
00200 if (s->is_keyframe) {
00201 s->blocks[blk_idx].pos = s->keyframedata + (get_bits_count(gb) / 8);
00202 s->blocks[blk_idx].size = block_size;
00203 }
00204 if (!s->color_depth) {
00205
00206
00207 for (k = 1; k <= s->diff_height; k++) {
00208 memcpy(s->frame.data[0] + x_pos * 3 +
00209 (s->image_height - y_pos - s->diff_start - k) * s->frame.linesize[0],
00210 line, width * 3);
00211
00212 line += width * 3;
00213 }
00214 } else {
00215
00216 decode_hybrid(s->tmpblock, s->frame.data[0],
00217 s->image_height - (y_pos + 1 + s->diff_start + s->diff_height),
00218 x_pos, s->diff_height, width,
00219 s->frame.linesize[0], s->pal);
00220 }
00221 skip_bits_long(gb, 8 * block_size);
00222 return 0;
00223 }
00224
00225 static int calc_deflate_block_size(int tmpblock_size)
00226 {
00227 z_stream zstream;
00228 int size;
00229
00230 zstream.zalloc = Z_NULL;
00231 zstream.zfree = Z_NULL;
00232 zstream.opaque = Z_NULL;
00233 if (deflateInit(&zstream, 0) != Z_OK)
00234 return -1;
00235 size = deflateBound(&zstream, tmpblock_size);
00236 deflateEnd(&zstream);
00237
00238 return size;
00239 }
00240
00241 static int flashsv_decode_frame(AVCodecContext *avctx, void *data,
00242 int *got_frame, AVPacket *avpkt)
00243 {
00244 int buf_size = avpkt->size;
00245 FlashSVContext *s = avctx->priv_data;
00246 int h_blocks, v_blocks, h_part, v_part, i, j;
00247 GetBitContext gb;
00248
00249
00250 if (buf_size == 0)
00251 return 0;
00252 if (buf_size < 4)
00253 return -1;
00254
00255 init_get_bits(&gb, avpkt->data, buf_size * 8);
00256
00257
00258 s->block_width = 16 * (get_bits(&gb, 4) + 1);
00259 s->image_width = get_bits(&gb, 12);
00260 s->block_height = 16 * (get_bits(&gb, 4) + 1);
00261 s->image_height = get_bits(&gb, 12);
00262
00263 if (s->ver == 2) {
00264 skip_bits(&gb, 6);
00265 if (get_bits1(&gb)) {
00266 av_log_missing_feature(avctx, "iframe", 1);
00267 return AVERROR_PATCHWELCOME;
00268 }
00269 if (get_bits1(&gb)) {
00270 av_log_missing_feature(avctx, "Custom palette", 1);
00271 return AVERROR_PATCHWELCOME;
00272 }
00273 }
00274
00275
00276 h_blocks = s->image_width / s->block_width;
00277 h_part = s->image_width % s->block_width;
00278 v_blocks = s->image_height / s->block_height;
00279 v_part = s->image_height % s->block_height;
00280
00281
00282
00283 if (s->block_size < s->block_width * s->block_height) {
00284 int tmpblock_size = 3 * s->block_width * s->block_height;
00285
00286 s->tmpblock = av_realloc(s->tmpblock, tmpblock_size);
00287 if (!s->tmpblock) {
00288 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00289 return AVERROR(ENOMEM);
00290 }
00291 if (s->ver == 2) {
00292 s->deflate_block_size = calc_deflate_block_size(tmpblock_size);
00293 if (s->deflate_block_size <= 0) {
00294 av_log(avctx, AV_LOG_ERROR, "Can't determine deflate buffer size.\n");
00295 return -1;
00296 }
00297 s->deflate_block = av_realloc(s->deflate_block, s->deflate_block_size);
00298 if (!s->deflate_block) {
00299 av_log(avctx, AV_LOG_ERROR, "Can't allocate deflate buffer.\n");
00300 return AVERROR(ENOMEM);
00301 }
00302 }
00303 }
00304 s->block_size = s->block_width * s->block_height;
00305
00306
00307 if (avctx->width == 0 && avctx->height == 0) {
00308 avcodec_set_dimensions(avctx, s->image_width, s->image_height);
00309 }
00310
00311
00312 if (avctx->width != s->image_width || avctx->height != s->image_height) {
00313 av_log(avctx, AV_LOG_ERROR,
00314 "Frame width or height differs from first frame!\n");
00315 av_log(avctx, AV_LOG_ERROR, "fh = %d, fv %d vs ch = %d, cv = %d\n",
00316 avctx->height, avctx->width, s->image_height, s->image_width);
00317 return AVERROR_INVALIDDATA;
00318 }
00319
00320
00321 s->is_keyframe = (avpkt->flags & AV_PKT_FLAG_KEY) && (s->ver == 2);
00322 if (s->is_keyframe) {
00323 s->keyframedata = av_realloc(s->keyframedata, avpkt->size);
00324 memcpy(s->keyframedata, avpkt->data, avpkt->size);
00325 }
00326 if(s->ver == 2)
00327 s->blocks = av_realloc(s->blocks,
00328 (v_blocks + !!v_part) * (h_blocks + !!h_part)
00329 * sizeof(s->blocks[0]));
00330
00331 av_dlog(avctx, "image: %dx%d block: %dx%d num: %dx%d part: %dx%d\n",
00332 s->image_width, s->image_height, s->block_width, s->block_height,
00333 h_blocks, v_blocks, h_part, v_part);
00334
00335 s->frame.reference = 3;
00336 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
00337 FF_BUFFER_HINTS_PRESERVE |
00338 FF_BUFFER_HINTS_REUSABLE;
00339 if (avctx->reget_buffer(avctx, &s->frame) < 0) {
00340 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00341 return -1;
00342 }
00343
00344
00345 for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
00346
00347 int y_pos = j * s->block_height;
00348 int cur_blk_height = (j < v_blocks) ? s->block_height : v_part;
00349
00350
00351 for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {
00352 int x_pos = i * s->block_width;
00353 int cur_blk_width = (i < h_blocks) ? s->block_width : h_part;
00354 int has_diff = 0;
00355
00356
00357 int size = get_bits(&gb, 16);
00358
00359 s->color_depth = 0;
00360 s->zlibprime_curr = 0;
00361 s->zlibprime_prev = 0;
00362 s->diff_start = 0;
00363 s->diff_height = cur_blk_height;
00364
00365 if (8 * size > get_bits_left(&gb)) {
00366 avctx->release_buffer(avctx, &s->frame);
00367 s->frame.data[0] = NULL;
00368 return AVERROR_INVALIDDATA;
00369 }
00370
00371 if (s->ver == 2 && size) {
00372 skip_bits(&gb, 3);
00373 s->color_depth = get_bits(&gb, 2);
00374 has_diff = get_bits1(&gb);
00375 s->zlibprime_curr = get_bits1(&gb);
00376 s->zlibprime_prev = get_bits1(&gb);
00377
00378 if (s->color_depth != 0 && s->color_depth != 2) {
00379 av_log(avctx, AV_LOG_ERROR,
00380 "%dx%d invalid color depth %d\n", i, j, s->color_depth);
00381 return AVERROR_INVALIDDATA;
00382 }
00383
00384 if (has_diff) {
00385 if (!s->keyframe) {
00386 av_log(avctx, AV_LOG_ERROR,
00387 "inter frame without keyframe\n");
00388 return AVERROR_INVALIDDATA;
00389 }
00390 s->diff_start = get_bits(&gb, 8);
00391 s->diff_height = get_bits(&gb, 8);
00392 av_log(avctx, AV_LOG_DEBUG,
00393 "%dx%d diff start %d height %d\n",
00394 i, j, s->diff_start, s->diff_height);
00395 size -= 2;
00396 }
00397
00398 if (s->zlibprime_prev)
00399 av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_prev\n", i, j);
00400
00401 if (s->zlibprime_curr) {
00402 int col = get_bits(&gb, 8);
00403 int row = get_bits(&gb, 8);
00404 av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_curr %dx%d\n", i, j, col, row);
00405 size -= 2;
00406 av_log_missing_feature(avctx, "zlibprime_curr", 1);
00407 return AVERROR_PATCHWELCOME;
00408 }
00409 if (!s->blocks && (s->zlibprime_curr || s->zlibprime_prev)) {
00410 av_log(avctx, AV_LOG_ERROR, "no data available for zlib "
00411 "priming\n");
00412 return AVERROR_INVALIDDATA;
00413 }
00414 size--;
00415 }
00416
00417 if (has_diff) {
00418 int k;
00419 int off = (s->image_height - y_pos - 1) * s->frame.linesize[0];
00420
00421 for (k = 0; k < cur_blk_height; k++)
00422 memcpy(s->frame.data[0] + off - k*s->frame.linesize[0] + x_pos*3,
00423 s->keyframe + off - k*s->frame.linesize[0] + x_pos*3,
00424 cur_blk_width * 3);
00425 }
00426
00427
00428 if (size) {
00429 if (flashsv_decode_block(avctx, avpkt, &gb, size,
00430 cur_blk_width, cur_blk_height,
00431 x_pos, y_pos,
00432 i + j * (h_blocks + !!h_part)))
00433 av_log(avctx, AV_LOG_ERROR,
00434 "error in decompression of block %dx%d\n", i, j);
00435 }
00436 }
00437 }
00438 if (s->is_keyframe && s->ver == 2) {
00439 if (!s->keyframe) {
00440 s->keyframe = av_malloc(s->frame.linesize[0] * avctx->height);
00441 if (!s->keyframe) {
00442 av_log(avctx, AV_LOG_ERROR, "Cannot allocate image data\n");
00443 return AVERROR(ENOMEM);
00444 }
00445 }
00446 memcpy(s->keyframe, s->frame.data[0], s->frame.linesize[0] * avctx->height);
00447 }
00448
00449 *got_frame = 1;
00450 *(AVFrame*)data = s->frame;
00451
00452 if ((get_bits_count(&gb) / 8) != buf_size)
00453 av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
00454 buf_size, (get_bits_count(&gb) / 8));
00455
00456
00457 return buf_size;
00458 }
00459
00460
00461 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
00462 {
00463 FlashSVContext *s = avctx->priv_data;
00464 inflateEnd(&s->zstream);
00465
00466 if (s->frame.data[0])
00467 avctx->release_buffer(avctx, &s->frame);
00468
00469
00470 av_free(s->tmpblock);
00471
00472 return 0;
00473 }
00474
00475
00476 #if CONFIG_FLASHSV_DECODER
00477 AVCodec ff_flashsv_decoder = {
00478 .name = "flashsv",
00479 .type = AVMEDIA_TYPE_VIDEO,
00480 .id = AV_CODEC_ID_FLASHSV,
00481 .priv_data_size = sizeof(FlashSVContext),
00482 .init = flashsv_decode_init,
00483 .close = flashsv_decode_end,
00484 .decode = flashsv_decode_frame,
00485 .capabilities = CODEC_CAP_DR1,
00486 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
00487 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v1"),
00488 };
00489 #endif
00490
00491 #if CONFIG_FLASHSV2_DECODER
00492 static const uint32_t ff_flashsv2_default_palette[128] = {
00493 0x000000, 0x333333, 0x666666, 0x999999, 0xCCCCCC, 0xFFFFFF,
00494 0x330000, 0x660000, 0x990000, 0xCC0000, 0xFF0000, 0x003300,
00495 0x006600, 0x009900, 0x00CC00, 0x00FF00, 0x000033, 0x000066,
00496 0x000099, 0x0000CC, 0x0000FF, 0x333300, 0x666600, 0x999900,
00497 0xCCCC00, 0xFFFF00, 0x003333, 0x006666, 0x009999, 0x00CCCC,
00498 0x00FFFF, 0x330033, 0x660066, 0x990099, 0xCC00CC, 0xFF00FF,
00499 0xFFFF33, 0xFFFF66, 0xFFFF99, 0xFFFFCC, 0xFF33FF, 0xFF66FF,
00500 0xFF99FF, 0xFFCCFF, 0x33FFFF, 0x66FFFF, 0x99FFFF, 0xCCFFFF,
00501 0xCCCC33, 0xCCCC66, 0xCCCC99, 0xCCCCFF, 0xCC33CC, 0xCC66CC,
00502 0xCC99CC, 0xCCFFCC, 0x33CCCC, 0x66CCCC, 0x99CCCC, 0xFFCCCC,
00503 0x999933, 0x999966, 0x9999CC, 0x9999FF, 0x993399, 0x996699,
00504 0x99CC99, 0x99FF99, 0x339999, 0x669999, 0xCC9999, 0xFF9999,
00505 0x666633, 0x666699, 0x6666CC, 0x6666FF, 0x663366, 0x669966,
00506 0x66CC66, 0x66FF66, 0x336666, 0x996666, 0xCC6666, 0xFF6666,
00507 0x333366, 0x333399, 0x3333CC, 0x3333FF, 0x336633, 0x339933,
00508 0x33CC33, 0x33FF33, 0x663333, 0x993333, 0xCC3333, 0xFF3333,
00509 0x003366, 0x336600, 0x660033, 0x006633, 0x330066, 0x663300,
00510 0x336699, 0x669933, 0x993366, 0x339966, 0x663399, 0x996633,
00511 0x6699CC, 0x99CC66, 0xCC6699, 0x66CC99, 0x9966CC, 0xCC9966,
00512 0x99CCFF, 0xCCFF99, 0xFF99CC, 0x99FFCC, 0xCC99FF, 0xFFCC99,
00513 0x111111, 0x222222, 0x444444, 0x555555, 0xAAAAAA, 0xBBBBBB,
00514 0xDDDDDD, 0xEEEEEE
00515 };
00516
00517 static av_cold int flashsv2_decode_init(AVCodecContext *avctx)
00518 {
00519 FlashSVContext *s = avctx->priv_data;
00520 flashsv_decode_init(avctx);
00521 s->pal = ff_flashsv2_default_palette;
00522 s->ver = 2;
00523
00524 return 0;
00525 }
00526
00527 static av_cold int flashsv2_decode_end(AVCodecContext *avctx)
00528 {
00529 FlashSVContext *s = avctx->priv_data;
00530
00531 av_freep(&s->keyframedata);
00532 av_freep(&s->blocks);
00533 av_freep(&s->keyframe);
00534 av_freep(&s->deflate_block);
00535 flashsv_decode_end(avctx);
00536
00537 return 0;
00538 }
00539
00540 AVCodec ff_flashsv2_decoder = {
00541 .name = "flashsv2",
00542 .type = AVMEDIA_TYPE_VIDEO,
00543 .id = AV_CODEC_ID_FLASHSV2,
00544 .priv_data_size = sizeof(FlashSVContext),
00545 .init = flashsv2_decode_init,
00546 .close = flashsv2_decode_end,
00547 .decode = flashsv_decode_frame,
00548 .capabilities = CODEC_CAP_DR1,
00549 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24, AV_PIX_FMT_NONE },
00550 .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video v2"),
00551 };
00552 #endif