2
* Zip Motion Blocks Video (ZMBV) decoder
3
* Copyright (c) 2006 Konstantin Shishkov
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
* Zip Motion Blocks Video decoder
38
#define ZMBV_KEYFRAME 1
39
#define ZMBV_DELTAPAL 2
56
typedef struct ZmbvContext {
57
AVCodecContext *avctx;
61
unsigned int decomp_size;
74
int (*decode_intra)(struct ZmbvContext *c);
75
int (*decode_xor)(struct ZmbvContext *c);
79
* Decode XOR'ed frame - 8bpp version
82
static int zmbv_decode_xor_8(ZmbvContext *c)
84
uint8_t *src = c->decomp_buf;
85
uint8_t *output, *prev;
88
int d, dx, dy, bw2, bh2;
96
if(c->flags & ZMBV_DELTAPAL){
97
for(i = 0; i < 768; i++)
102
src += ((c->bx * c->by * 2 + 3) & ~3);
105
for(y = 0; y < c->height; y += c->bh) {
106
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
107
for(x = 0; x < c->width; x += c->bw) {
108
uint8_t *out, *tprev;
111
dx = mvec[block] >> 1;
112
dy = mvec[block + 1] >> 1;
115
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
117
/* copy block - motion vectors out of bounds are used to zero blocks */
119
tprev = prev + x + dx + dy * c->width;
122
for(j = 0; j < bh2; j++){
123
if((my + j < 0) || (my + j >= c->height)) {
126
for(i = 0; i < bw2; i++){
127
if((mx + i < 0) || (mx + i >= c->width))
137
if(d) { /* apply XOR'ed difference */
139
for(j = 0; j < bh2; j++){
140
for(i = 0; i < bw2; i++)
146
output += c->width * c->bh;
147
prev += c->width * c->bh;
149
if(src - c->decomp_buf != c->decomp_len)
150
av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
155
* Decode XOR'ed frame - 15bpp and 16bpp version
158
static int zmbv_decode_xor_16(ZmbvContext *c)
160
uint8_t *src = c->decomp_buf;
161
uint16_t *output, *prev;
164
int d, dx, dy, bw2, bh2;
169
output = (uint16_t*)c->cur;
170
prev = (uint16_t*)c->prev;
173
src += ((c->bx * c->by * 2 + 3) & ~3);
176
for(y = 0; y < c->height; y += c->bh) {
177
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
178
for(x = 0; x < c->width; x += c->bw) {
179
uint16_t *out, *tprev;
182
dx = mvec[block] >> 1;
183
dy = mvec[block + 1] >> 1;
186
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
188
/* copy block - motion vectors out of bounds are used to zero blocks */
190
tprev = prev + x + dx + dy * c->width;
193
for(j = 0; j < bh2; j++){
194
if((my + j < 0) || (my + j >= c->height)) {
195
memset(out, 0, bw2 * 2);
197
for(i = 0; i < bw2; i++){
198
if((mx + i < 0) || (mx + i >= c->width))
208
if(d) { /* apply XOR'ed difference */
210
for(j = 0; j < bh2; j++){
211
for(i = 0; i < bw2; i++) {
212
out[i] ^= *((uint16_t*)src);
219
output += c->width * c->bh;
220
prev += c->width * c->bh;
222
if(src - c->decomp_buf != c->decomp_len)
223
av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
227
#ifdef ZMBV_ENABLE_24BPP
229
* Decode XOR'ed frame - 24bpp version
232
static int zmbv_decode_xor_24(ZmbvContext *c)
234
uint8_t *src = c->decomp_buf;
235
uint8_t *output, *prev;
238
int d, dx, dy, bw2, bh2;
247
stride = c->width * 3;
249
src += ((c->bx * c->by * 2 + 3) & ~3);
252
for(y = 0; y < c->height; y += c->bh) {
253
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
254
for(x = 0; x < c->width; x += c->bw) {
255
uint8_t *out, *tprev;
258
dx = mvec[block] >> 1;
259
dy = mvec[block + 1] >> 1;
262
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
264
/* copy block - motion vectors out of bounds are used to zero blocks */
265
out = output + x * 3;
266
tprev = prev + (x + dx) * 3 + dy * stride;
269
for(j = 0; j < bh2; j++){
270
if((my + j < 0) || (my + j >= c->height)) {
271
memset(out, 0, bw2 * 3);
273
for(i = 0; i < bw2; i++){
274
if((mx + i < 0) || (mx + i >= c->width)) {
279
out[i * 3 + 0] = tprev[i * 3 + 0];
280
out[i * 3 + 1] = tprev[i * 3 + 1];
281
out[i * 3 + 2] = tprev[i * 3 + 2];
289
if(d) { /* apply XOR'ed difference */
290
out = output + x * 3;
291
for(j = 0; j < bh2; j++){
292
for(i = 0; i < bw2; i++) {
293
out[i * 3 + 0] ^= *src++;
294
out[i * 3 + 1] ^= *src++;
295
out[i * 3 + 2] ^= *src++;
301
output += stride * c->bh;
302
prev += stride * c->bh;
304
if(src - c->decomp_buf != c->decomp_len)
305
av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
308
#endif //ZMBV_ENABLE_24BPP
311
* Decode XOR'ed frame - 32bpp version
314
static int zmbv_decode_xor_32(ZmbvContext *c)
316
uint8_t *src = c->decomp_buf;
317
uint32_t *output, *prev;
320
int d, dx, dy, bw2, bh2;
325
output = (uint32_t*)c->cur;
326
prev = (uint32_t*)c->prev;
329
src += ((c->bx * c->by * 2 + 3) & ~3);
332
for(y = 0; y < c->height; y += c->bh) {
333
bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
334
for(x = 0; x < c->width; x += c->bw) {
335
uint32_t *out, *tprev;
338
dx = mvec[block] >> 1;
339
dy = mvec[block + 1] >> 1;
342
bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
344
/* copy block - motion vectors out of bounds are used to zero blocks */
346
tprev = prev + x + dx + dy * c->width;
349
for(j = 0; j < bh2; j++){
350
if((my + j < 0) || (my + j >= c->height)) {
351
memset(out, 0, bw2 * 4);
353
for(i = 0; i < bw2; i++){
354
if((mx + i < 0) || (mx + i >= c->width))
364
if(d) { /* apply XOR'ed difference */
366
for(j = 0; j < bh2; j++){
367
for(i = 0; i < bw2; i++) {
368
out[i] ^= *((uint32_t*)src);
375
output += c->width * c->bh;
376
prev += c->width * c->bh;
378
if(src - c->decomp_buf != c->decomp_len)
379
av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
386
static int zmbv_decode_intra(ZmbvContext *c)
388
uint8_t *src = c->decomp_buf;
390
/* make the palette available on the way out */
391
if (c->fmt == ZMBV_FMT_8BPP) {
392
memcpy(c->pal, src, 768);
396
memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
400
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
402
ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
405
int zret = Z_OK; // Zlib return code
411
avctx->release_buffer(avctx, &c->pic);
413
c->pic.reference = 1;
414
c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
415
if(avctx->get_buffer(avctx, &c->pic) < 0){
416
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
420
outptr = c->pic.data[0]; // Output image pointer
425
if(c->flags & ZMBV_KEYFRAME) {
435
av_log(avctx, AV_LOG_DEBUG, "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
436
if(hi_ver != 0 || lo_ver != 1) {
437
av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
440
if(c->bw == 0 || c->bh == 0) {
441
av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
443
if(c->comp != 0 && c->comp != 1) {
444
av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
451
c->decode_intra = zmbv_decode_intra;
452
c->decode_xor = zmbv_decode_xor_8;
457
c->decode_intra = zmbv_decode_intra;
458
c->decode_xor = zmbv_decode_xor_16;
460
#ifdef ZMBV_ENABLE_24BPP
463
c->decode_intra = zmbv_decode_intra;
464
c->decode_xor = zmbv_decode_xor_24;
466
#endif //ZMBV_ENABLE_24BPP
469
c->decode_intra = zmbv_decode_intra;
470
c->decode_xor = zmbv_decode_xor_32;
473
c->decode_intra = NULL;
474
c->decode_xor = NULL;
475
av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
479
zret = inflateReset(&c->zstream);
481
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
485
av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
487
#endif /* CONFIG_ZLIB */
488
c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
489
c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
490
c->bx = (c->width + c->bw - 1) / c->bw;
491
c->by = (c->height+ c->bh - 1) / c->bh;
494
if(c->decode_intra == NULL) {
495
av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
499
if(c->comp == 0) { //Uncompressed data
500
memcpy(c->decomp_buf, buf, len);
502
} else { // ZLIB-compressed data
504
c->zstream.total_in = c->zstream.total_out = 0;
505
c->zstream.next_in = buf;
506
c->zstream.avail_in = len;
507
c->zstream.next_out = c->decomp_buf;
508
c->zstream.avail_out = c->decomp_size;
509
inflate(&c->zstream, Z_FINISH);
510
c->decomp_len = c->zstream.total_out;
512
av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
516
if(c->flags & ZMBV_KEYFRAME) {
517
c->pic.key_frame = 1;
518
c->pic.pict_type = FF_I_TYPE;
521
c->pic.key_frame = 0;
522
c->pic.pict_type = FF_P_TYPE;
531
out = c->pic.data[0];
535
for(j = 0; j < c->height; j++) {
536
for(i = 0; i < c->width; i++) {
537
out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
538
out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
539
out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
542
out += c->pic.linesize[0];
546
for(j = 0; j < c->height; j++) {
547
for(i = 0; i < c->width; i++) {
548
uint16_t tmp = AV_RL16(src);
550
out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
551
out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
552
out[i * 3 + 2] = (tmp & 0x001F) << 3;
554
out += c->pic.linesize[0];
558
for(j = 0; j < c->height; j++) {
559
for(i = 0; i < c->width; i++) {
560
uint16_t tmp = AV_RL16(src);
562
out[i * 3 + 0] = (tmp & 0xF800) >> 8;
563
out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
564
out[i * 3 + 2] = (tmp & 0x001F) << 3;
566
out += c->pic.linesize[0];
569
#ifdef ZMBV_ENABLE_24BPP
571
for(j = 0; j < c->height; j++) {
572
memcpy(out, src, c->width * 3);
574
out += c->pic.linesize[0];
577
#endif //ZMBV_ENABLE_24BPP
579
for(j = 0; j < c->height; j++) {
580
for(i = 0; i < c->width; i++) {
581
uint32_t tmp = AV_RL32(src);
583
out[i * 3 + 0] = tmp >> 16;
584
out[i * 3 + 1] = tmp >> 8;
585
out[i * 3 + 2] = tmp >> 0;
587
out += c->pic.linesize[0];
591
av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
593
memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8));
595
*data_size = sizeof(AVFrame);
596
*(AVFrame*)data = c->pic;
598
/* always report that the buffer was completely consumed */
609
static int decode_init(AVCodecContext *avctx)
611
ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
612
int zret; // Zlib return code
615
avctx->has_b_frames = 0;
617
c->pic.data[0] = NULL;
618
c->width = avctx->width;
619
c->height = avctx->height;
621
if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
624
c->bpp = avctx->bits_per_sample;
627
// Needed if zlib unused or init aborted before inflateInit
628
memset(&(c->zstream), 0, sizeof(z_stream));
630
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
633
avctx->pix_fmt = PIX_FMT_RGB24;
634
c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
636
/* Allocate decompression buffer */
637
if (c->decomp_size) {
638
if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
639
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
645
c->zstream.zalloc = Z_NULL;
646
c->zstream.zfree = Z_NULL;
647
c->zstream.opaque = Z_NULL;
648
zret = inflateInit(&(c->zstream));
650
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
662
* Uninit zmbv decoder
665
static int decode_end(AVCodecContext *avctx)
667
ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
669
av_freep(&c->decomp_buf);
672
avctx->release_buffer(avctx, &c->pic);
674
inflateEnd(&(c->zstream));
682
AVCodec zmbv_decoder = {