]> git.sesse.net Git - ffmpeg/blob - libavcodec/lcl.c
fix warning
[ffmpeg] / libavcodec / lcl.c
1 /*
2  * LCL (LossLess Codec Library) Codec
3  * Copyright (c) 2002-2004 Roberto Togni
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file lcl.c
24  * LCL (LossLess Codec Library) Video Codec
25  * Decoder for MSZH and ZLIB codecs
26  * Experimental encoder for ZLIB RGB24
27  *
28  * Fourcc: MSZH, ZLIB
29  *
30  * Original Win32 dll:
31  * Ver2.23 By Kenji Oshima 2000.09.20
32  * avimszh.dll, avizlib.dll
33  *
34  * A description of the decoding algorithm can be found here:
35  *   http://www.pcisys.net/~melanson/codecs
36  *
37  * Supports: BGR24 (RGB 24bpp)
38  *
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include "avcodec.h"
45 #include "bitstream.h"
46
47 #ifdef CONFIG_ZLIB
48 #include <zlib.h>
49 #endif
50
51
52 #define BMPTYPE_YUV 1
53 #define BMPTYPE_RGB 2
54
55 #define IMGTYPE_YUV111 0
56 #define IMGTYPE_YUV422 1
57 #define IMGTYPE_RGB24 2
58 #define IMGTYPE_YUV411 3
59 #define IMGTYPE_YUV211 4
60 #define IMGTYPE_YUV420 5
61
62 #define COMP_MSZH 0
63 #define COMP_MSZH_NOCOMP 1
64 #define COMP_ZLIB_HISPEED 1
65 #define COMP_ZLIB_HICOMP 9
66 #define COMP_ZLIB_NORMAL -1
67
68 #define FLAG_MULTITHREAD 1
69 #define FLAG_NULLFRAME 2
70 #define FLAG_PNGFILTER 4
71 #define FLAGMASK_UNUSED 0xf8
72
73 #define CODEC_MSZH 1
74 #define CODEC_ZLIB 3
75
76 #define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
77 #define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
78
79 /*
80  * Decoder context
81  */
82 typedef struct LclContext {
83
84         AVCodecContext *avctx;
85         AVFrame pic;
86     PutBitContext pb;
87
88     // Image type
89     int imgtype;
90     // Compression type
91     int compression;
92     // Flags
93     int flags;
94     // Decompressed data size
95     unsigned int decomp_size;
96     // Decompression buffer
97     unsigned char* decomp_buf;
98     // Maximum compressed data size
99     unsigned int max_comp_size;
100     // Compression buffer
101     unsigned char* comp_buf;
102 #ifdef CONFIG_ZLIB
103     z_stream zstream;
104 #endif
105 } LclContext;
106
107
108 /*
109  *
110  * Helper functions
111  *
112  */
113 static inline unsigned char fix (int pix14)
114 {
115     int tmp;
116
117     tmp = (pix14 + 0x80000) >> 20;
118     if (tmp < 0)
119         return 0;
120     if (tmp > 255)
121         return 255;
122     return tmp;
123 }
124
125
126
127 static inline unsigned char get_b (unsigned char yq, signed char bq)
128 {
129     return fix((yq << 20) + bq * 1858076);
130 }
131
132
133
134 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
135 {
136     return fix((yq << 20) - bq * 360857 - rq * 748830);
137 }
138
139
140
141 static inline unsigned char get_r (unsigned char yq, signed char rq)
142 {
143     return fix((yq << 20) + rq * 1470103);
144 }
145
146
147
148 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
149 {
150     unsigned char *destptr_bak = destptr;
151     unsigned char *destptr_end = destptr + destsize;
152     unsigned char mask = 0;
153     unsigned char maskbit = 0;
154     unsigned int ofs, cnt;
155
156     while ((srclen > 0) && (destptr < destptr_end)) {
157         if (maskbit == 0) {
158             mask = *(srcptr++);
159             maskbit = 8;
160             srclen--;
161             continue;
162         }
163         if ((mask & (1 << (--maskbit))) == 0) {
164             if (destptr + 4 > destptr_end)
165                 break;
166             *(int*)destptr = *(int*)srcptr;
167             srclen -= 4;
168             destptr += 4;
169             srcptr += 4;
170         } else {
171             ofs = *(srcptr++);
172             cnt = *(srcptr++);
173             ofs += cnt * 256;;
174             cnt = ((cnt >> 3) & 0x1f) + 1;
175             ofs &= 0x7ff;
176             srclen -= 2;
177             cnt *= 4;
178             if (destptr + cnt > destptr_end) {
179                 cnt =  destptr_end - destptr;
180             }
181             for (; cnt > 0; cnt--) {
182                 *(destptr) = *(destptr - ofs);
183                 destptr++;
184             }
185         }
186     }
187
188     return (destptr - destptr_bak);
189 }
190
191
192
193 #ifdef CONFIG_DECODERS
194 /*
195  *
196  * Decode a frame
197  *
198  */
199 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
200 {
201         LclContext * const c = avctx->priv_data;
202         unsigned char *encoded = (unsigned char *)buf;
203     unsigned int pixel_ptr;
204     int row, col;
205     unsigned char *outptr;
206     unsigned int width = avctx->width; // Real image width
207     unsigned int height = avctx->height; // Real image height
208     unsigned int mszh_dlen;
209     unsigned char yq, y1q, uq, vq;
210     int uqvq;
211     unsigned int mthread_inlen, mthread_outlen;
212 #ifdef CONFIG_ZLIB
213     int zret; // Zlib return code
214 #endif
215     unsigned int len = buf_size;
216
217         if(c->pic.data[0])
218                 avctx->release_buffer(avctx, &c->pic);
219
220         c->pic.reference = 0;
221         c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
222         if(avctx->get_buffer(avctx, &c->pic) < 0){
223                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
224                 return -1;
225         }
226
227     outptr = c->pic.data[0]; // Output image pointer
228
229     /* Decompress frame */
230     switch (avctx->codec_id) {
231         case CODEC_ID_MSZH:
232             switch (c->compression) {
233                 case COMP_MSZH:
234                     if (c->flags & FLAG_MULTITHREAD) {
235                         mthread_inlen = *((unsigned int*)encoded);
236                         mthread_outlen = *((unsigned int*)(encoded+4));
237                         if (mthread_outlen > c->decomp_size) // this should not happen
238                             mthread_outlen = c->decomp_size;
239                         mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
240                         if (mthread_outlen != mszh_dlen) {
241                             av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
242                                    mthread_outlen, mszh_dlen);
243                             return -1;
244                         }
245                         mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
246                                                 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
247                         if (mthread_outlen != mszh_dlen) {
248                             av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
249                                    mthread_outlen, mszh_dlen);
250                             return -1;
251                         }
252                         encoded = c->decomp_buf;
253                         len = c->decomp_size;
254                     } else {
255                         mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
256                         if (c->decomp_size != mszh_dlen) {
257                             av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
258                                    c->decomp_size, mszh_dlen);
259                             return -1;
260                         }
261                         encoded = c->decomp_buf;
262                         len = mszh_dlen;
263                     }
264                     break;
265                 case COMP_MSZH_NOCOMP:
266                     break;
267                 default:
268                     av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
269                     return -1;
270             }
271             break;
272         case CODEC_ID_ZLIB:
273 #ifdef CONFIG_ZLIB
274             /* Using the original dll with normal compression (-1) and RGB format
275              * gives a file with ZLIB fourcc, but frame is really uncompressed.
276              * To be sure that's true check also frame size */
277             if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
278                (len == width * height * 3))
279                 break;
280             zret = inflateReset(&(c->zstream));
281             if (zret != Z_OK) {
282                 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
283                 return -1;
284             }
285             if (c->flags & FLAG_MULTITHREAD) {
286                 mthread_inlen = *((unsigned int*)encoded);
287                 mthread_outlen = *((unsigned int*)(encoded+4));
288                 if (mthread_outlen > c->decomp_size)
289                     mthread_outlen = c->decomp_size;
290                 c->zstream.next_in = encoded + 8;
291                 c->zstream.avail_in = mthread_inlen;
292                 c->zstream.next_out = c->decomp_buf;
293                 c->zstream.avail_out = c->decomp_size;
294                 zret = inflate(&(c->zstream), Z_FINISH);
295                 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
296                     av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
297                     return -1;
298                 }
299                 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
300                     av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
301                            mthread_outlen, c->zstream.total_out);
302                     return -1;
303                 }
304                 zret = inflateReset(&(c->zstream));
305                 if (zret != Z_OK) {
306                     av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
307                     return -1;
308                 }
309                 c->zstream.next_in = encoded + 8 + mthread_inlen;
310                 c->zstream.avail_in = len - mthread_inlen;
311                 c->zstream.next_out = c->decomp_buf + mthread_outlen;
312                 c->zstream.avail_out = c->decomp_size - mthread_outlen;
313                 zret = inflate(&(c->zstream), Z_FINISH);
314                 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
315                     av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
316                     return -1;
317                 }
318                 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
319                     av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
320                            mthread_outlen, c->zstream.total_out);
321                     return -1;
322                 }
323             } else {
324                 c->zstream.next_in = encoded;
325                 c->zstream.avail_in = len;
326                 c->zstream.next_out = c->decomp_buf;
327                 c->zstream.avail_out = c->decomp_size;
328                 zret = inflate(&(c->zstream), Z_FINISH);
329                 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
330                     av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
331                     return -1;
332                 }
333                 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
334                     av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
335                            c->decomp_size, c->zstream.total_out);
336                     return -1;
337                 }
338             }
339             encoded = c->decomp_buf;
340             len = c->decomp_size;;
341 #else
342             av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
343             return -1;
344 #endif
345             break;
346         default:
347             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
348             return -1;
349     }
350
351
352     /* Apply PNG filter */
353     if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
354         switch (c->imgtype) {
355             case IMGTYPE_YUV111:
356             case IMGTYPE_RGB24:
357                 for (row = 0; row < height; row++) {
358                     pixel_ptr = row * width * 3;
359                     yq = encoded[pixel_ptr++];
360                     uqvq = AV_RL16(encoded+pixel_ptr);
361                     pixel_ptr += 2;
362                     for (col = 1; col < width; col++) {
363                         encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
364                         uqvq -= AV_RL16(encoded+pixel_ptr+1);
365                         AV_WL16(encoded+pixel_ptr+1, uqvq);
366                         pixel_ptr += 3;
367                     }
368                 }
369                 break;
370             case IMGTYPE_YUV422:
371                 for (row = 0; row < height; row++) {
372                     pixel_ptr = row * width * 2;
373                     yq = uq = vq =0;
374                     for (col = 0; col < width/4; col++) {
375                         encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
376                         encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
377                         encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
378                         encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
379                         encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
380                         encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
381                         encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
382                         encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
383                         pixel_ptr += 8;
384                     }
385                 }
386                 break;
387             case IMGTYPE_YUV411:
388                 for (row = 0; row < height; row++) {
389                     pixel_ptr = row * width / 2 * 3;
390                     yq = uq = vq =0;
391                     for (col = 0; col < width/4; col++) {
392                         encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
393                         encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
394                         encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
395                         encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
396                         encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
397                         encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
398                         pixel_ptr += 6;
399                     }
400                 }
401                 break;
402             case IMGTYPE_YUV211:
403                 for (row = 0; row < height; row++) {
404                     pixel_ptr = row * width * 2;
405                     yq = uq = vq =0;
406                     for (col = 0; col < width/2; col++) {
407                         encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
408                         encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
409                         encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
410                         encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
411                         pixel_ptr += 4;
412                     }
413                 }
414                 break;
415             case IMGTYPE_YUV420:
416                 for (row = 0; row < height/2; row++) {
417                     pixel_ptr = row * width * 3;
418                     yq = y1q = uq = vq =0;
419                     for (col = 0; col < width/2; col++) {
420                         encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
421                         encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
422                         encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
423                         encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
424                         encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
425                         encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
426                         pixel_ptr += 6;
427                     }
428                 }
429                 break;
430             default:
431                 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
432                 return -1;
433         }
434     }
435
436     /* Convert colorspace */
437     switch (c->imgtype) {
438         case IMGTYPE_YUV111:
439             for (row = height - 1; row >= 0; row--) {
440                 pixel_ptr = row * c->pic.linesize[0];
441                 for (col = 0; col < width; col++) {
442                     outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
443                     outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
444                     outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
445                     encoded += 3;
446                 }
447             }
448             break;
449         case IMGTYPE_YUV422:
450             for (row = height - 1; row >= 0; row--) {
451                 pixel_ptr = row * c->pic.linesize[0];
452                 for (col = 0; col < width/4; col++) {
453                     outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
454                     outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
455                     outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
456                     outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
457                     outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
458                     outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
459                     outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
460                     outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
461                     outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
462                     outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
463                     outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
464                     outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
465                     encoded += 8;
466                 }
467             }
468             break;
469         case IMGTYPE_RGB24:
470             for (row = height - 1; row >= 0; row--) {
471                 pixel_ptr = row * c->pic.linesize[0];
472                 for (col = 0; col < width; col++) {
473                     outptr[pixel_ptr++] = encoded[0];
474                     outptr[pixel_ptr++] = encoded[1];
475                     outptr[pixel_ptr++] = encoded[2];
476                     encoded += 3;
477                 }
478             }
479             break;
480         case IMGTYPE_YUV411:
481             for (row = height - 1; row >= 0; row--) {
482                 pixel_ptr = row * c->pic.linesize[0];
483                 for (col = 0; col < width/4; col++) {
484                     outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
485                     outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
486                     outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
487                     outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
488                     outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
489                     outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
490                     outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
491                     outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
492                     outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
493                     outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
494                     outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
495                     outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
496                     encoded += 6;
497                 }
498             }
499             break;
500         case IMGTYPE_YUV211:
501             for (row = height - 1; row >= 0; row--) {
502                 pixel_ptr = row * c->pic.linesize[0];
503                 for (col = 0; col < width/2; col++) {
504                     outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
505                     outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
506                     outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
507                     outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
508                     outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
509                     outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
510                     encoded += 4;
511                 }
512             }
513             break;
514         case IMGTYPE_YUV420:
515             for (row = height / 2 - 1; row >= 0; row--) {
516                 pixel_ptr = 2 * row * c->pic.linesize[0];
517                 for (col = 0; col < width/2; col++) {
518                     outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
519                     outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
520                     outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
521                     outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
522                     outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
523                     outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
524                     outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
525                     outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
526                     outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
527                     outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
528                     outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
529                     outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
530                     pixel_ptr += 6;
531                     encoded += 6;
532                 }
533             }
534             break;
535         default:
536             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
537             return -1;
538     }
539
540     *data_size = sizeof(AVFrame);
541     *(AVFrame*)data = c->pic;
542
543     /* always report that the buffer was completely consumed */
544     return buf_size;
545 }
546 #endif
547
548 #ifdef CONFIG_ENCODERS
549 /*
550  *
551  * Encode a frame
552  *
553  */
554 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
555     LclContext *c = avctx->priv_data;
556     AVFrame *pict = data;
557     AVFrame * const p = &c->pic;
558     int i;
559     int zret; // Zlib return code
560
561 #ifndef CONFIG_ZLIB
562     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
563     return -1;
564 #else
565
566     init_put_bits(&c->pb, buf, buf_size);
567
568     *p = *pict;
569     p->pict_type= FF_I_TYPE;
570     p->key_frame= 1;
571
572     if(avctx->pix_fmt != PIX_FMT_BGR24){
573         av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
574         return -1;
575     }
576
577     zret = deflateReset(&(c->zstream));
578     if (zret != Z_OK) {
579         av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
580         return -1;
581     }
582     c->zstream.next_out = c->comp_buf;
583     c->zstream.avail_out = c->max_comp_size;
584
585     for(i = avctx->height - 1; i >= 0; i--) {
586         c->zstream.next_in = p->data[0]+p->linesize[0]*i;
587         c->zstream.avail_in = avctx->width*3;
588         zret = deflate(&(c->zstream), Z_NO_FLUSH);
589         if (zret != Z_OK) {
590             av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
591             return -1;
592         }
593     }
594     zret = deflate(&(c->zstream), Z_FINISH);
595     if (zret != Z_STREAM_END) {
596         av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
597         return -1;
598     }
599
600     for (i = 0; i < c->zstream.total_out; i++)
601         put_bits(&c->pb, 8, c->comp_buf[i]);
602     flush_put_bits(&c->pb);
603
604     return c->zstream.total_out;
605 #endif
606 }
607 #endif /* CONFIG_ENCODERS */
608
609 #ifdef CONFIG_DECODERS
610 /*
611  *
612  * Init lcl decoder
613  *
614  */
615 static int decode_init(AVCodecContext *avctx)
616 {
617     LclContext * const c = avctx->priv_data;
618     unsigned int basesize = avctx->width * avctx->height;
619     unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
620     unsigned int max_decomp_size;
621     int zret; // Zlib return code
622
623     c->avctx = avctx;
624
625     c->pic.data[0] = NULL;
626
627 #ifdef CONFIG_ZLIB
628     // Needed if zlib unused or init aborted before inflateInit
629     memset(&(c->zstream), 0, sizeof(z_stream));
630 #endif
631
632     if (avctx->extradata_size < 8) {
633         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
634         return 1;
635     }
636
637     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
638         return 1;
639     }
640
641     /* Check codec type */
642     if (((avctx->codec_id == CODEC_ID_MSZH)  && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
643         ((avctx->codec_id == CODEC_ID_ZLIB)  && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
644         av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
645     }
646
647     /* Detect image type */
648     switch (c->imgtype = *((char *)avctx->extradata + 4)) {
649         case IMGTYPE_YUV111:
650             c->decomp_size = basesize * 3;
651             max_decomp_size = max_basesize * 3;
652             av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
653             break;
654         case IMGTYPE_YUV422:
655             c->decomp_size = basesize * 2;
656             max_decomp_size = max_basesize * 2;
657             av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
658             break;
659         case IMGTYPE_RGB24:
660             c->decomp_size = basesize * 3;
661             max_decomp_size = max_basesize * 3;
662             av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
663             break;
664         case IMGTYPE_YUV411:
665             c->decomp_size = basesize / 2 * 3;
666             max_decomp_size = max_basesize / 2 * 3;
667             av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
668             break;
669         case IMGTYPE_YUV211:
670             c->decomp_size = basesize * 2;
671             max_decomp_size = max_basesize * 2;
672             av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
673             break;
674         case IMGTYPE_YUV420:
675             c->decomp_size = basesize / 2 * 3;
676             max_decomp_size = max_basesize / 2 * 3;
677             av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
678             break;
679         default:
680             av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
681             return 1;
682     }
683
684     /* Detect compression method */
685     c->compression = *((char *)avctx->extradata + 5);
686     switch (avctx->codec_id) {
687         case CODEC_ID_MSZH:
688             switch (c->compression) {
689                 case COMP_MSZH:
690                     av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
691                     break;
692                 case COMP_MSZH_NOCOMP:
693                     c->decomp_size = 0;
694                     av_log(avctx, AV_LOG_INFO, "No compression.\n");
695                     break;
696                 default:
697                     av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
698                     return 1;
699             }
700             break;
701         case CODEC_ID_ZLIB:
702 #ifdef CONFIG_ZLIB
703             switch (c->compression) {
704                 case COMP_ZLIB_HISPEED:
705                     av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
706                     break;
707                 case COMP_ZLIB_HICOMP:
708                     av_log(avctx, AV_LOG_INFO, "High compression.\n");
709                     break;
710                 case COMP_ZLIB_NORMAL:
711                     av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
712                     break;
713                 default:
714                     if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
715                             av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
716                         return 1;
717                     }
718                     av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
719             }
720 #else
721             av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
722             return 1;
723 #endif
724             break;
725         default:
726             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
727             return 1;
728     }
729
730     /* Allocate decompression buffer */
731     if (c->decomp_size) {
732         if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
733             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
734             return 1;
735         }
736     }
737
738     /* Detect flags */
739     c->flags = *((char *)avctx->extradata + 6);
740     if (c->flags & FLAG_MULTITHREAD)
741         av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
742     if (c->flags & FLAG_NULLFRAME)
743         av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
744     if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
745         av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
746     if (c->flags & FLAGMASK_UNUSED)
747         av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
748
749     /* If needed init zlib */
750     if (avctx->codec_id == CODEC_ID_ZLIB) {
751 #ifdef CONFIG_ZLIB
752         c->zstream.zalloc = Z_NULL;
753         c->zstream.zfree = Z_NULL;
754         c->zstream.opaque = Z_NULL;
755         zret = inflateInit(&(c->zstream));
756         if (zret != Z_OK) {
757             av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
758             return 1;
759         }
760 #else
761     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
762     return 1;
763 #endif
764     }
765
766     avctx->pix_fmt = PIX_FMT_BGR24;
767
768     return 0;
769 }
770 #endif /* CONFIG_DECODERS */
771
772 #ifdef CONFIG_ENCODERS
773 /*
774  *
775  * Init lcl encoder
776  *
777  */
778 static int encode_init(AVCodecContext *avctx)
779 {
780     LclContext *c = avctx->priv_data;
781     int zret; // Zlib return code
782
783 #ifndef CONFIG_ZLIB
784     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
785     return 1;
786 #else
787
788     c->avctx= avctx;
789
790     assert(avctx->width && avctx->height);
791
792     avctx->extradata= av_mallocz(8);
793     avctx->coded_frame= &c->pic;
794
795     // Will be user settable someday
796     c->compression = 6;
797     c->flags = 0;
798
799     switch(avctx->pix_fmt){
800         case PIX_FMT_BGR24:
801             c->imgtype = IMGTYPE_RGB24;
802             c->decomp_size = avctx->width * avctx->height * 3;
803             avctx->bits_per_sample= 24;
804             break;
805         default:
806             av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
807             return -1;
808     }
809
810     ((uint8_t*)avctx->extradata)[0]= 4;
811     ((uint8_t*)avctx->extradata)[1]= 0;
812     ((uint8_t*)avctx->extradata)[2]= 0;
813     ((uint8_t*)avctx->extradata)[3]= 0;
814     ((uint8_t*)avctx->extradata)[4]= c->imgtype;
815     ((uint8_t*)avctx->extradata)[5]= c->compression;
816     ((uint8_t*)avctx->extradata)[6]= c->flags;
817     ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
818     c->avctx->extradata_size= 8;
819
820     c->zstream.zalloc = Z_NULL;
821     c->zstream.zfree = Z_NULL;
822     c->zstream.opaque = Z_NULL;
823     zret = deflateInit(&(c->zstream), c->compression);
824     if (zret != Z_OK) {
825         av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
826         return 1;
827     }
828
829         /* Conservative upper bound taken from zlib v1.2.1 source */
830         c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
831                            ((c->decomp_size + 63) >> 6) + 11;
832     if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
833         av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
834         return 1;
835     }
836
837     return 0;
838 #endif
839 }
840 #endif /* CONFIG_ENCODERS */
841
842
843
844 #ifdef CONFIG_DECODERS
845 /*
846  *
847  * Uninit lcl decoder
848  *
849  */
850 static int decode_end(AVCodecContext *avctx)
851 {
852         LclContext * const c = avctx->priv_data;
853
854         if (c->pic.data[0])
855                 avctx->release_buffer(avctx, &c->pic);
856 #ifdef CONFIG_ZLIB
857     inflateEnd(&(c->zstream));
858 #endif
859
860         return 0;
861 }
862 #endif
863
864 #ifdef CONFIG_ENCODERS
865 /*
866  *
867  * Uninit lcl encoder
868  *
869  */
870 static int encode_end(AVCodecContext *avctx)
871 {
872     LclContext *c = avctx->priv_data;
873
874     av_freep(&avctx->extradata);
875     av_freep(&c->comp_buf);
876 #ifdef CONFIG_ZLIB
877     deflateEnd(&(c->zstream));
878 #endif
879
880     return 0;
881 }
882 #endif
883
884 #ifdef CONFIG_MSZH_DECODER
885 AVCodec mszh_decoder = {
886         "mszh",
887         CODEC_TYPE_VIDEO,
888         CODEC_ID_MSZH,
889         sizeof(LclContext),
890         decode_init,
891         NULL,
892         decode_end,
893         decode_frame,
894         CODEC_CAP_DR1,
895 };
896 #endif
897
898 #ifdef CONFIG_ZLIB_DECODER
899 AVCodec zlib_decoder = {
900         "zlib",
901         CODEC_TYPE_VIDEO,
902         CODEC_ID_ZLIB,
903         sizeof(LclContext),
904         decode_init,
905         NULL,
906         decode_end,
907         decode_frame,
908         CODEC_CAP_DR1,
909 };
910 #endif
911
912 #ifdef CONFIG_ENCODERS
913
914 AVCodec zlib_encoder = {
915     "zlib",
916     CODEC_TYPE_VIDEO,
917     CODEC_ID_ZLIB,
918     sizeof(LclContext),
919     encode_init,
920     encode_frame,
921     encode_end,
922 };
923
924 #endif //CONFIG_ENCODERS