]> git.sesse.net Git - ffmpeg/blob - libavcodec/zmbv.c
fdca4d2a2803043a3d07d964d7c0a4587d61ec72
[ffmpeg] / libavcodec / zmbv.c
1 /*
2  * Zip Motion Blocks Video (ZMBV) decoder
3  * Copyright (c) 2006 Konstantin Shishkov
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20
21 /**
22  * @file zmbv.c
23  * Zip Motion Blocks Video decoder
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "common.h"
30 #include "avcodec.h"
31
32 #ifdef CONFIG_ZLIB
33 #include <zlib.h>
34 #endif
35
36 #define ZMBV_KEYFRAME 1
37 #define ZMBV_DELTAPAL 2
38
39 enum ZmbvFormat {
40     ZMBV_FMT_NONE  = 0,
41     ZMBV_FMT_1BPP  = 1,
42     ZMBV_FMT_2BPP  = 2,
43     ZMBV_FMT_4BPP  = 3,
44     ZMBV_FMT_8BPP  = 4,
45     ZMBV_FMT_15BPP = 5,
46     ZMBV_FMT_16BPP = 6,
47     ZMBV_FMT_24BPP = 7,
48     ZMBV_FMT_32BPP = 8
49 };
50
51 /*
52  * Decoder context
53  */
54 typedef struct ZmbvContext {
55     AVCodecContext *avctx;
56     AVFrame pic;
57
58     int bpp;
59     unsigned int decomp_size;
60     uint8_t* decomp_buf;
61     uint8_t pal[768];
62     uint8_t *prev, *cur;
63     int width, height;
64     int fmt;
65     int comp;
66     int flags;
67     int bw, bh, bx, by;
68     int decomp_len;
69 #ifdef CONFIG_ZLIB
70     z_stream zstream;
71 #endif
72     int (*decode_intra)(struct ZmbvContext *c);
73     int (*decode_xor)(struct ZmbvContext *c);
74 } ZmbvContext;
75
76 /**
77  * Decode XOR'ed frame - 8bpp version
78  */
79
80 static int zmbv_decode_xor_8(ZmbvContext *c)
81 {
82     uint8_t *src = c->decomp_buf;
83     uint8_t *output, *prev;
84     int8_t *mvec;
85     int x, y;
86     int d, dx, dy, bw2, bh2;
87     int block;
88     int i, j;
89     int mx, my;
90
91     output = c->cur;
92     prev = c->prev;
93
94     if(c->flags & ZMBV_DELTAPAL){
95         for(i = 0; i < 768; i++)
96             c->pal[i] ^= *src++;
97     }
98
99     mvec = (int8_t*)src;
100     src += ((c->bx * c->by * 2 + 3) & ~3);
101
102     block = 0;
103     for(y = 0; y < c->height; y += c->bh) {
104         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
105         for(x = 0; x < c->width; x += c->bw) {
106             uint8_t *out, *tprev;
107
108             d = mvec[block] & 1;
109             dx = mvec[block] >> 1;
110             dy = mvec[block + 1] >> 1;
111             block += 2;
112
113             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
114
115             /* copy block - motion vectors out of bounds are used to zero blocks */
116             out = output + x;
117             tprev = prev + x + dx + dy * c->width;
118             mx = x + dx;
119             my = y + dy;
120             for(j = 0; j < bh2; j++){
121                 if((my + j < 0) || (my + j >= c->height)) {
122                     memset(out, 0, bw2);
123                 } else {
124                     for(i = 0; i < bw2; i++){
125                         if((mx + i < 0) || (mx + i >= c->width))
126                             out[i] = 0;
127                         else
128                             out[i] = tprev[i];
129                     }
130                 }
131                 out += c->width;
132                 tprev += c->width;
133             }
134
135             if(d) { /* apply XOR'ed difference */
136                 out = output + x;
137                 for(j = 0; j < bh2; j++){
138                     for(i = 0; i < bw2; i++)
139                         out[i] ^= *src++;
140                     out += c->width;
141                 }
142             }
143         }
144         output += c->width * c->bh;
145         prev += c->width * c->bh;
146     }
147     if(src - c->decomp_buf != c->decomp_len)
148         av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
149     return 0;
150 }
151
152 /**
153  * Decode XOR'ed frame - 15bpp and 16bpp version
154  */
155
156 static int zmbv_decode_xor_16(ZmbvContext *c)
157 {
158     uint8_t *src = c->decomp_buf;
159     uint16_t *output, *prev;
160     int8_t *mvec;
161     int x, y;
162     int d, dx, dy, bw2, bh2;
163     int block;
164     int i, j;
165     int mx, my;
166
167     output = (uint16_t*)c->cur;
168     prev = (uint16_t*)c->prev;
169
170     mvec = (int8_t*)src;
171     src += ((c->bx * c->by * 2 + 3) & ~3);
172
173     block = 0;
174     for(y = 0; y < c->height; y += c->bh) {
175         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
176         for(x = 0; x < c->width; x += c->bw) {
177             uint16_t *out, *tprev;
178
179             d = mvec[block] & 1;
180             dx = mvec[block] >> 1;
181             dy = mvec[block + 1] >> 1;
182             block += 2;
183
184             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
185
186             /* copy block - motion vectors out of bounds are used to zero blocks */
187             out = output + x;
188             tprev = prev + x + dx + dy * c->width;
189             mx = x + dx;
190             my = y + dy;
191             for(j = 0; j < bh2; j++){
192                 if((my + j < 0) || (my + j >= c->height)) {
193                     memset(out, 0, bw2 * 2);
194                 } else {
195                     for(i = 0; i < bw2; i++){
196                         if((mx + i < 0) || (mx + i >= c->width))
197                             out[i] = 0;
198                         else
199                             out[i] = tprev[i];
200                     }
201                 }
202                 out += c->width;
203                 tprev += c->width;
204             }
205
206             if(d) { /* apply XOR'ed difference */
207                 out = output + x;
208                 for(j = 0; j < bh2; j++){
209                     for(i = 0; i < bw2; i++) {
210                         out[i] ^= *((uint16_t*)src);
211                         src += 2;
212                     }
213                     out += c->width;
214                 }
215             }
216         }
217         output += c->width * c->bh;
218         prev += c->width * c->bh;
219     }
220     if(src - c->decomp_buf != c->decomp_len)
221         av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
222     return 0;
223 }
224
225 #ifdef ZMBV_ENABLE_24BPP
226 /**
227  * Decode XOR'ed frame - 24bpp version
228  */
229
230 static int zmbv_decode_xor_24(ZmbvContext *c)
231 {
232     uint8_t *src = c->decomp_buf;
233     uint8_t *output, *prev;
234     int8_t *mvec;
235     int x, y;
236     int d, dx, dy, bw2, bh2;
237     int block;
238     int i, j;
239     int mx, my;
240     int stride;
241
242     output = c->cur;
243     prev = c->prev;
244
245     stride = c->width * 3;
246     mvec = (int8_t*)src;
247     src += ((c->bx * c->by * 2 + 3) & ~3);
248
249     block = 0;
250     for(y = 0; y < c->height; y += c->bh) {
251         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
252         for(x = 0; x < c->width; x += c->bw) {
253             uint8_t *out, *tprev;
254
255             d = mvec[block] & 1;
256             dx = mvec[block] >> 1;
257             dy = mvec[block + 1] >> 1;
258             block += 2;
259
260             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
261
262             /* copy block - motion vectors out of bounds are used to zero blocks */
263             out = output + x * 3;
264             tprev = prev + (x + dx) * 3 + dy * stride;
265             mx = x + dx;
266             my = y + dy;
267             for(j = 0; j < bh2; j++){
268                 if((my + j < 0) || (my + j >= c->height)) {
269                     memset(out, 0, bw2 * 3);
270                 } else {
271                     for(i = 0; i < bw2; i++){
272                         if((mx + i < 0) || (mx + i >= c->width)) {
273                             out[i * 3 + 0] = 0;
274                             out[i * 3 + 1] = 0;
275                             out[i * 3 + 2] = 0;
276                         } else {
277                             out[i * 3 + 0] = tprev[i * 3 + 0];
278                             out[i * 3 + 1] = tprev[i * 3 + 1];
279                             out[i * 3 + 2] = tprev[i * 3 + 2];
280                         }
281                     }
282                 }
283                 out += stride;
284                 tprev += stride;
285             }
286
287             if(d) { /* apply XOR'ed difference */
288                 out = output + x * 3;
289                 for(j = 0; j < bh2; j++){
290                     for(i = 0; i < bw2; i++) {
291                         out[i * 3 + 0] ^= *src++;
292                         out[i * 3 + 1] ^= *src++;
293                         out[i * 3 + 2] ^= *src++;
294                     }
295                     out += stride;
296                 }
297             }
298         }
299         output += stride * c->bh;
300         prev += stride * c->bh;
301     }
302     if(src - c->decomp_buf != c->decomp_len)
303         av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
304     return 0;
305 }
306 #endif //ZMBV_ENABLE_24BPP
307
308 /**
309  * Decode XOR'ed frame - 32bpp version
310  */
311
312 static int zmbv_decode_xor_32(ZmbvContext *c)
313 {
314     uint8_t *src = c->decomp_buf;
315     uint32_t *output, *prev;
316     int8_t *mvec;
317     int x, y;
318     int d, dx, dy, bw2, bh2;
319     int block;
320     int i, j;
321     int mx, my;
322
323     output = (uint32_t*)c->cur;
324     prev = (uint32_t*)c->prev;
325
326     mvec = (int8_t*)src;
327     src += ((c->bx * c->by * 2 + 3) & ~3);
328
329     block = 0;
330     for(y = 0; y < c->height; y += c->bh) {
331         bh2 = ((c->height - y) > c->bh) ? c->bh : (c->height - y);
332         for(x = 0; x < c->width; x += c->bw) {
333             uint32_t *out, *tprev;
334
335             d = mvec[block] & 1;
336             dx = mvec[block] >> 1;
337             dy = mvec[block + 1] >> 1;
338             block += 2;
339
340             bw2 = ((c->width - x) > c->bw) ? c->bw : (c->width - x);
341
342             /* copy block - motion vectors out of bounds are used to zero blocks */
343             out = output + x;
344             tprev = prev + x + dx + dy * c->width;
345             mx = x + dx;
346             my = y + dy;
347             for(j = 0; j < bh2; j++){
348                 if((my + j < 0) || (my + j >= c->height)) {
349                     memset(out, 0, bw2 * 4);
350                 } else {
351                     for(i = 0; i < bw2; i++){
352                         if((mx + i < 0) || (mx + i >= c->width))
353                             out[i] = 0;
354                         else
355                             out[i] = tprev[i];
356                     }
357                 }
358                 out += c->width;
359                 tprev += c->width;
360             }
361
362             if(d) { /* apply XOR'ed difference */
363                 out = output + x;
364                 for(j = 0; j < bh2; j++){
365                     for(i = 0; i < bw2; i++) {
366                         out[i] ^= *((uint32_t*)src);
367                         src += 4;
368                     }
369                     out += c->width;
370                 }
371             }
372         }
373         output += c->width * c->bh;
374         prev += c->width * c->bh;
375     }
376     if(src - c->decomp_buf != c->decomp_len)
377         av_log(c->avctx, AV_LOG_ERROR, "Used %i of %i bytes\n", src-c->decomp_buf, c->decomp_len);
378     return 0;
379 }
380
381 /**
382  * Decode intraframe
383  */
384 static int zmbv_decode_intra(ZmbvContext *c)
385 {
386     uint8_t *src = c->decomp_buf;
387
388     /* make the palette available on the way out */
389     if (c->fmt == ZMBV_FMT_8BPP) {
390         memcpy(c->pal, src, 768);
391         src += 768;
392     }
393
394     memcpy(c->cur, src, c->width * c->height * (c->bpp / 8));
395     return 0;
396 }
397
398 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
399 {
400     ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
401     uint8_t *outptr;
402 #ifdef CONFIG_ZLIB
403     int zret = Z_OK; // Zlib return code
404 #endif
405     int len = buf_size;
406     int hi_ver, lo_ver;
407
408     if(c->pic.data[0])
409             avctx->release_buffer(avctx, &c->pic);
410
411     c->pic.reference = 1;
412     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
413     if(avctx->get_buffer(avctx, &c->pic) < 0){
414         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
415         return -1;
416     }
417
418     outptr = c->pic.data[0]; // Output image pointer
419
420     /* parse header */
421     c->flags = buf[0];
422     buf++; len--;
423     if(c->flags & ZMBV_KEYFRAME) {
424         hi_ver = buf[0];
425         lo_ver = buf[1];
426         c->comp = buf[2];
427         c->fmt = buf[3];
428         c->bw = buf[4];
429         c->bh = buf[5];
430
431         buf += 6;
432         len -= 6;
433         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);
434         if(hi_ver != 0 || lo_ver != 1) {
435             av_log(avctx, AV_LOG_ERROR, "Unsupported version %i.%i\n", hi_ver, lo_ver);
436             return -1;
437         }
438         if(c->bw == 0 || c->bh == 0) {
439             av_log(avctx, AV_LOG_ERROR, "Unsupported block size %ix%i\n", c->bw, c->bh);
440         }
441         if(c->comp != 0 && c->comp != 1) {
442             av_log(avctx, AV_LOG_ERROR, "Unsupported compression type %i\n", c->comp);
443             return -1;
444         }
445
446         switch(c->fmt) {
447         case ZMBV_FMT_8BPP:
448             c->bpp = 8;
449             c->decode_intra = zmbv_decode_intra;
450             c->decode_xor = zmbv_decode_xor_8;
451             break;
452         case ZMBV_FMT_15BPP:
453         case ZMBV_FMT_16BPP:
454             c->bpp = 16;
455             c->decode_intra = zmbv_decode_intra;
456             c->decode_xor = zmbv_decode_xor_16;
457             break;
458 #ifdef ZMBV_ENABLE_24BPP
459         case ZMBV_FMT_24BPP:
460             c->bpp = 24;
461             c->decode_intra = zmbv_decode_intra;
462             c->decode_xor = zmbv_decode_xor_24;
463             break;
464 #endif //ZMBV_ENABLE_24BPP
465         case ZMBV_FMT_32BPP:
466             c->bpp = 32;
467             c->decode_intra = zmbv_decode_intra;
468             c->decode_xor = zmbv_decode_xor_32;
469             break;
470         default:
471             c->decode_intra = NULL;
472             c->decode_xor = NULL;
473             av_log(avctx, AV_LOG_ERROR, "Unsupported (for now) format %i\n", c->fmt);
474             return -1;
475         }
476 #ifdef CONFIG_ZLIB
477         zret = inflateReset(&c->zstream);
478         if (zret != Z_OK) {
479             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
480             return -1;
481         }
482 #else
483         av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
484         return -1;
485 #endif  /* CONFIG_ZLIB */
486         c->cur = av_realloc(c->cur, avctx->width * avctx->height * (c->bpp / 8));
487         c->prev = av_realloc(c->prev, avctx->width * avctx->height * (c->bpp / 8));
488         c->bx = (c->width + c->bw - 1) / c->bw;
489         c->by = (c->height+ c->bh - 1) / c->bh;
490     }
491
492     if(c->decode_intra == NULL) {
493         av_log(avctx, AV_LOG_ERROR, "Error! Got no format or no keyframe!\n");
494         return -1;
495     }
496
497     if(c->comp == 0) { //Uncompressed data
498         memcpy(c->decomp_buf, buf, len);
499         c->decomp_size = 1;
500     } else { // ZLIB-compressed data
501 #ifdef CONFIG_ZLIB
502         c->zstream.total_in = c->zstream.total_out = 0;
503         c->zstream.next_in = buf;
504         c->zstream.avail_in = len;
505         c->zstream.next_out = c->decomp_buf;
506         c->zstream.avail_out = c->decomp_size;
507         inflate(&c->zstream, Z_FINISH);
508         c->decomp_len = c->zstream.total_out;
509 #else
510         av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
511         return -1;
512 #endif
513     }
514     if(c->flags & ZMBV_KEYFRAME) {
515         c->pic.key_frame = 1;
516         c->pic.pict_type = FF_I_TYPE;
517         c->decode_intra(c);
518     } else {
519         c->pic.key_frame = 0;
520         c->pic.pict_type = FF_P_TYPE;
521         c->decode_xor(c);
522     }
523
524     /* update frames */
525     {
526         uint8_t *out, *src;
527         int i, j;
528
529         out = c->pic.data[0];
530         src = c->cur;
531         switch(c->fmt) {
532         case ZMBV_FMT_8BPP:
533             for(j = 0; j < c->height; j++) {
534                 for(i = 0; i < c->width; i++) {
535                     out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
536                     out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
537                     out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
538                     *src++;
539                 }
540                 out += c->pic.linesize[0];
541             }
542             break;
543         case ZMBV_FMT_15BPP:
544             for(j = 0; j < c->height; j++) {
545                 for(i = 0; i < c->width; i++) {
546                     uint16_t tmp = LE_16(src);
547                     src += 2;
548                     out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
549                     out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
550                     out[i * 3 + 2] = (tmp & 0x001F) << 3;
551                 }
552                 out += c->pic.linesize[0];
553             }
554             break;
555         case ZMBV_FMT_16BPP:
556             for(j = 0; j < c->height; j++) {
557                 for(i = 0; i < c->width; i++) {
558                     uint16_t tmp = LE_16(src);
559                     src += 2;
560                     out[i * 3 + 0] = (tmp & 0xF800) >> 8;
561                     out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
562                     out[i * 3 + 2] = (tmp & 0x001F) << 3;
563                 }
564                 out += c->pic.linesize[0];
565             }
566             break;
567 #ifdef ZMBV_ENABLE_24BPP
568         case ZMBV_FMT_24BPP:
569             for(j = 0; j < c->height; j++) {
570                 memcpy(out, src, c->width * 3);
571                 src += c->width * 3;
572                 out += c->pic.linesize[0];
573             }
574             break;
575 #endif //ZMBV_ENABLE_24BPP
576         case ZMBV_FMT_32BPP:
577             for(j = 0; j < c->height; j++) {
578                 for(i = 0; i < c->width; i++) {
579                     uint32_t tmp = LE_32(src);
580                     src += 4;
581                     out[i * 3 + 0] = tmp >> 16;
582                     out[i * 3 + 1] = tmp >> 8;
583                     out[i * 3 + 2] = tmp >> 0;
584                 }
585                 out += c->pic.linesize[0];
586             }
587             break;
588         default:
589             av_log(avctx, AV_LOG_ERROR, "Cannot handle format %i\n", c->fmt);
590         }
591         memcpy(c->prev, c->cur, c->width * c->height * (c->bpp / 8));
592     }
593     *data_size = sizeof(AVFrame);
594     *(AVFrame*)data = c->pic;
595
596     /* always report that the buffer was completely consumed */
597     return buf_size;
598 }
599
600
601
602 /*
603  *
604  * Init zmbv decoder
605  *
606  */
607 static int decode_init(AVCodecContext *avctx)
608 {
609     ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
610     int zret; // Zlib return code
611
612     c->avctx = avctx;
613     avctx->has_b_frames = 0;
614
615     c->pic.data[0] = NULL;
616     c->width = avctx->width;
617     c->height = avctx->height;
618
619     if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
620         return 1;
621     }
622     c->bpp = avctx->bits_per_sample;
623
624 #ifdef CONFIG_ZLIB
625     // Needed if zlib unused or init aborted before inflateInit
626     memset(&(c->zstream), 0, sizeof(z_stream));
627 #else
628     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
629     return 1;
630 #endif
631     avctx->pix_fmt = PIX_FMT_RGB24;
632     c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
633
634     /* Allocate decompression buffer */
635     if (c->decomp_size) {
636         if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
637             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
638             return 1;
639         }
640     }
641
642 #ifdef CONFIG_ZLIB
643     c->zstream.zalloc = Z_NULL;
644     c->zstream.zfree = Z_NULL;
645     c->zstream.opaque = Z_NULL;
646     zret = inflateInit(&(c->zstream));
647     if (zret != Z_OK) {
648         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
649         return 1;
650     }
651 #endif
652
653     return 0;
654 }
655
656
657
658 /*
659  *
660  * Uninit zmbv decoder
661  *
662  */
663 static int decode_end(AVCodecContext *avctx)
664 {
665     ZmbvContext * const c = (ZmbvContext *)avctx->priv_data;
666
667     av_freep(&c->decomp_buf);
668
669     if (c->pic.data[0])
670         avctx->release_buffer(avctx, &c->pic);
671 #ifdef CONFIG_ZLIB
672     inflateEnd(&(c->zstream));
673 #endif
674     av_freep(&c->cur);
675     av_freep(&c->prev);
676
677     return 0;
678 }
679
680 AVCodec zmbv_decoder = {
681     "zmbv",
682     CODEC_TYPE_VIDEO,
683     CODEC_ID_ZMBV,
684     sizeof(ZmbvContext),
685     decode_init,
686     NULL,
687     decode_end,
688     decode_frame
689 };
690