comparison lcldec.c @ 5294:e21873a1c00b libavcodec

split lcl.c into lcldec.c, lclenc.c, lcl.h
author mru
date Thu, 12 Jul 2007 22:40:33 +0000
parents lcl.c@2b72f9bc4f06
children 01e09790f4e6
comparison
equal deleted inserted replaced
5293:de5b524db473 5294:e21873a1c00b
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 #include "lcl.h"
47
48 #ifdef CONFIG_ZLIB
49 #include <zlib.h>
50 #endif
51
52 /*
53 * Decoder context
54 */
55 typedef struct LclDecContext {
56
57 AVCodecContext *avctx;
58 AVFrame pic;
59
60 // Image type
61 int imgtype;
62 // Compression type
63 int compression;
64 // Flags
65 int flags;
66 // Decompressed data size
67 unsigned int decomp_size;
68 // Decompression buffer
69 unsigned char* decomp_buf;
70 #ifdef CONFIG_ZLIB
71 z_stream zstream;
72 #endif
73 } LclDecContext;
74
75
76 /*
77 *
78 * Helper functions
79 *
80 */
81 static inline unsigned char fix (int pix14)
82 {
83 int tmp;
84
85 tmp = (pix14 + 0x80000) >> 20;
86 if (tmp < 0)
87 return 0;
88 if (tmp > 255)
89 return 255;
90 return tmp;
91 }
92
93
94
95 static inline unsigned char get_b (unsigned char yq, signed char bq)
96 {
97 return fix((yq << 20) + bq * 1858076);
98 }
99
100
101
102 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
103 {
104 return fix((yq << 20) - bq * 360857 - rq * 748830);
105 }
106
107
108
109 static inline unsigned char get_r (unsigned char yq, signed char rq)
110 {
111 return fix((yq << 20) + rq * 1470103);
112 }
113
114
115
116 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
117 {
118 unsigned char *destptr_bak = destptr;
119 unsigned char *destptr_end = destptr + destsize;
120 unsigned char mask = 0;
121 unsigned char maskbit = 0;
122 unsigned int ofs, cnt;
123
124 while ((srclen > 0) && (destptr < destptr_end)) {
125 if (maskbit == 0) {
126 mask = *(srcptr++);
127 maskbit = 8;
128 srclen--;
129 continue;
130 }
131 if ((mask & (1 << (--maskbit))) == 0) {
132 if (destptr + 4 > destptr_end)
133 break;
134 *(int*)destptr = *(int*)srcptr;
135 srclen -= 4;
136 destptr += 4;
137 srcptr += 4;
138 } else {
139 ofs = *(srcptr++);
140 cnt = *(srcptr++);
141 ofs += cnt * 256;;
142 cnt = ((cnt >> 3) & 0x1f) + 1;
143 ofs &= 0x7ff;
144 srclen -= 2;
145 cnt *= 4;
146 if (destptr + cnt > destptr_end) {
147 cnt = destptr_end - destptr;
148 }
149 for (; cnt > 0; cnt--) {
150 *(destptr) = *(destptr - ofs);
151 destptr++;
152 }
153 }
154 }
155
156 return (destptr - destptr_bak);
157 }
158
159
160
161 /*
162 *
163 * Decode a frame
164 *
165 */
166 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
167 {
168 LclDecContext * const c = avctx->priv_data;
169 unsigned char *encoded = (unsigned char *)buf;
170 unsigned int pixel_ptr;
171 int row, col;
172 unsigned char *outptr;
173 unsigned int width = avctx->width; // Real image width
174 unsigned int height = avctx->height; // Real image height
175 unsigned int mszh_dlen;
176 unsigned char yq, y1q, uq, vq;
177 int uqvq;
178 unsigned int mthread_inlen, mthread_outlen;
179 #ifdef CONFIG_ZLIB
180 int zret; // Zlib return code
181 #endif
182 unsigned int len = buf_size;
183
184 if(c->pic.data[0])
185 avctx->release_buffer(avctx, &c->pic);
186
187 c->pic.reference = 0;
188 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
189 if(avctx->get_buffer(avctx, &c->pic) < 0){
190 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
191 return -1;
192 }
193
194 outptr = c->pic.data[0]; // Output image pointer
195
196 /* Decompress frame */
197 switch (avctx->codec_id) {
198 case CODEC_ID_MSZH:
199 switch (c->compression) {
200 case COMP_MSZH:
201 if (c->flags & FLAG_MULTITHREAD) {
202 mthread_inlen = *((unsigned int*)encoded);
203 mthread_outlen = *((unsigned int*)(encoded+4));
204 if (mthread_outlen > c->decomp_size) // this should not happen
205 mthread_outlen = c->decomp_size;
206 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
207 if (mthread_outlen != mszh_dlen) {
208 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
209 mthread_outlen, mszh_dlen);
210 return -1;
211 }
212 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
213 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
214 if (mthread_outlen != mszh_dlen) {
215 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
216 mthread_outlen, mszh_dlen);
217 return -1;
218 }
219 encoded = c->decomp_buf;
220 len = c->decomp_size;
221 } else {
222 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
223 if (c->decomp_size != mszh_dlen) {
224 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
225 c->decomp_size, mszh_dlen);
226 return -1;
227 }
228 encoded = c->decomp_buf;
229 len = mszh_dlen;
230 }
231 break;
232 case COMP_MSZH_NOCOMP:
233 break;
234 default:
235 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
236 return -1;
237 }
238 break;
239 case CODEC_ID_ZLIB:
240 #ifdef CONFIG_ZLIB
241 /* Using the original dll with normal compression (-1) and RGB format
242 * gives a file with ZLIB fourcc, but frame is really uncompressed.
243 * To be sure that's true check also frame size */
244 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
245 (len == width * height * 3))
246 break;
247 zret = inflateReset(&(c->zstream));
248 if (zret != Z_OK) {
249 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
250 return -1;
251 }
252 if (c->flags & FLAG_MULTITHREAD) {
253 mthread_inlen = *((unsigned int*)encoded);
254 mthread_outlen = *((unsigned int*)(encoded+4));
255 if (mthread_outlen > c->decomp_size)
256 mthread_outlen = c->decomp_size;
257 c->zstream.next_in = encoded + 8;
258 c->zstream.avail_in = mthread_inlen;
259 c->zstream.next_out = c->decomp_buf;
260 c->zstream.avail_out = c->decomp_size;
261 zret = inflate(&(c->zstream), Z_FINISH);
262 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
263 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
264 return -1;
265 }
266 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
267 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
268 mthread_outlen, c->zstream.total_out);
269 return -1;
270 }
271 zret = inflateReset(&(c->zstream));
272 if (zret != Z_OK) {
273 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
274 return -1;
275 }
276 c->zstream.next_in = encoded + 8 + mthread_inlen;
277 c->zstream.avail_in = len - mthread_inlen;
278 c->zstream.next_out = c->decomp_buf + mthread_outlen;
279 c->zstream.avail_out = c->decomp_size - mthread_outlen;
280 zret = inflate(&(c->zstream), Z_FINISH);
281 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
282 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
283 return -1;
284 }
285 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
286 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
287 mthread_outlen, c->zstream.total_out);
288 return -1;
289 }
290 } else {
291 c->zstream.next_in = encoded;
292 c->zstream.avail_in = len;
293 c->zstream.next_out = c->decomp_buf;
294 c->zstream.avail_out = c->decomp_size;
295 zret = inflate(&(c->zstream), Z_FINISH);
296 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
297 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
298 return -1;
299 }
300 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
301 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
302 c->decomp_size, c->zstream.total_out);
303 return -1;
304 }
305 }
306 encoded = c->decomp_buf;
307 len = c->decomp_size;;
308 #else
309 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
310 return -1;
311 #endif
312 break;
313 default:
314 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
315 return -1;
316 }
317
318
319 /* Apply PNG filter */
320 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
321 switch (c->imgtype) {
322 case IMGTYPE_YUV111:
323 case IMGTYPE_RGB24:
324 for (row = 0; row < height; row++) {
325 pixel_ptr = row * width * 3;
326 yq = encoded[pixel_ptr++];
327 uqvq = AV_RL16(encoded+pixel_ptr);
328 pixel_ptr += 2;
329 for (col = 1; col < width; col++) {
330 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
331 uqvq -= AV_RL16(encoded+pixel_ptr+1);
332 AV_WL16(encoded+pixel_ptr+1, uqvq);
333 pixel_ptr += 3;
334 }
335 }
336 break;
337 case IMGTYPE_YUV422:
338 for (row = 0; row < height; row++) {
339 pixel_ptr = row * width * 2;
340 yq = uq = vq =0;
341 for (col = 0; col < width/4; col++) {
342 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
343 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
344 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
345 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
346 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
347 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
348 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
349 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
350 pixel_ptr += 8;
351 }
352 }
353 break;
354 case IMGTYPE_YUV411:
355 for (row = 0; row < height; row++) {
356 pixel_ptr = row * width / 2 * 3;
357 yq = uq = vq =0;
358 for (col = 0; col < width/4; col++) {
359 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
360 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
361 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
362 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
363 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
364 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
365 pixel_ptr += 6;
366 }
367 }
368 break;
369 case IMGTYPE_YUV211:
370 for (row = 0; row < height; row++) {
371 pixel_ptr = row * width * 2;
372 yq = uq = vq =0;
373 for (col = 0; col < width/2; col++) {
374 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
375 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
376 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
377 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
378 pixel_ptr += 4;
379 }
380 }
381 break;
382 case IMGTYPE_YUV420:
383 for (row = 0; row < height/2; row++) {
384 pixel_ptr = row * width * 3;
385 yq = y1q = uq = vq =0;
386 for (col = 0; col < width/2; col++) {
387 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
388 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
389 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
390 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
391 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
392 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
393 pixel_ptr += 6;
394 }
395 }
396 break;
397 default:
398 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
399 return -1;
400 }
401 }
402
403 /* Convert colorspace */
404 switch (c->imgtype) {
405 case IMGTYPE_YUV111:
406 for (row = height - 1; row >= 0; row--) {
407 pixel_ptr = row * c->pic.linesize[0];
408 for (col = 0; col < width; col++) {
409 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
410 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
411 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
412 encoded += 3;
413 }
414 }
415 break;
416 case IMGTYPE_YUV422:
417 for (row = height - 1; row >= 0; row--) {
418 pixel_ptr = row * c->pic.linesize[0];
419 for (col = 0; col < width/4; col++) {
420 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
421 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
422 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
423 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
424 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
425 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
426 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
427 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
428 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
429 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
430 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
431 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
432 encoded += 8;
433 }
434 }
435 break;
436 case IMGTYPE_RGB24:
437 for (row = height - 1; row >= 0; row--) {
438 pixel_ptr = row * c->pic.linesize[0];
439 for (col = 0; col < width; col++) {
440 outptr[pixel_ptr++] = encoded[0];
441 outptr[pixel_ptr++] = encoded[1];
442 outptr[pixel_ptr++] = encoded[2];
443 encoded += 3;
444 }
445 }
446 break;
447 case IMGTYPE_YUV411:
448 for (row = height - 1; row >= 0; row--) {
449 pixel_ptr = row * c->pic.linesize[0];
450 for (col = 0; col < width/4; col++) {
451 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
452 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
453 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
454 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
455 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
456 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
457 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
458 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
459 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
460 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
461 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
462 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
463 encoded += 6;
464 }
465 }
466 break;
467 case IMGTYPE_YUV211:
468 for (row = height - 1; row >= 0; row--) {
469 pixel_ptr = row * c->pic.linesize[0];
470 for (col = 0; col < width/2; col++) {
471 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
472 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
473 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
474 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
475 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
476 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
477 encoded += 4;
478 }
479 }
480 break;
481 case IMGTYPE_YUV420:
482 for (row = height / 2 - 1; row >= 0; row--) {
483 pixel_ptr = 2 * row * c->pic.linesize[0];
484 for (col = 0; col < width/2; col++) {
485 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
486 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
487 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
488 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
489 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
490 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
491 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
492 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
493 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
494 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
495 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
496 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
497 pixel_ptr += 6;
498 encoded += 6;
499 }
500 }
501 break;
502 default:
503 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
504 return -1;
505 }
506
507 *data_size = sizeof(AVFrame);
508 *(AVFrame*)data = c->pic;
509
510 /* always report that the buffer was completely consumed */
511 return buf_size;
512 }
513
514 /*
515 *
516 * Init lcl decoder
517 *
518 */
519 static int decode_init(AVCodecContext *avctx)
520 {
521 LclDecContext * const c = avctx->priv_data;
522 unsigned int basesize = avctx->width * avctx->height;
523 unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
524 unsigned int max_decomp_size;
525 int zret; // Zlib return code
526
527 c->avctx = avctx;
528
529 c->pic.data[0] = NULL;
530
531 #ifdef CONFIG_ZLIB
532 // Needed if zlib unused or init aborted before inflateInit
533 memset(&(c->zstream), 0, sizeof(z_stream));
534 #endif
535
536 if (avctx->extradata_size < 8) {
537 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
538 return 1;
539 }
540
541 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
542 return 1;
543 }
544
545 /* Check codec type */
546 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
547 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
548 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
549 }
550
551 /* Detect image type */
552 switch (c->imgtype = *((char *)avctx->extradata + 4)) {
553 case IMGTYPE_YUV111:
554 c->decomp_size = basesize * 3;
555 max_decomp_size = max_basesize * 3;
556 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
557 break;
558 case IMGTYPE_YUV422:
559 c->decomp_size = basesize * 2;
560 max_decomp_size = max_basesize * 2;
561 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
562 break;
563 case IMGTYPE_RGB24:
564 c->decomp_size = basesize * 3;
565 max_decomp_size = max_basesize * 3;
566 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
567 break;
568 case IMGTYPE_YUV411:
569 c->decomp_size = basesize / 2 * 3;
570 max_decomp_size = max_basesize / 2 * 3;
571 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
572 break;
573 case IMGTYPE_YUV211:
574 c->decomp_size = basesize * 2;
575 max_decomp_size = max_basesize * 2;
576 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
577 break;
578 case IMGTYPE_YUV420:
579 c->decomp_size = basesize / 2 * 3;
580 max_decomp_size = max_basesize / 2 * 3;
581 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
582 break;
583 default:
584 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
585 return 1;
586 }
587
588 /* Detect compression method */
589 c->compression = *((char *)avctx->extradata + 5);
590 switch (avctx->codec_id) {
591 case CODEC_ID_MSZH:
592 switch (c->compression) {
593 case COMP_MSZH:
594 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
595 break;
596 case COMP_MSZH_NOCOMP:
597 c->decomp_size = 0;
598 av_log(avctx, AV_LOG_INFO, "No compression.\n");
599 break;
600 default:
601 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
602 return 1;
603 }
604 break;
605 case CODEC_ID_ZLIB:
606 #ifdef CONFIG_ZLIB
607 switch (c->compression) {
608 case COMP_ZLIB_HISPEED:
609 av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
610 break;
611 case COMP_ZLIB_HICOMP:
612 av_log(avctx, AV_LOG_INFO, "High compression.\n");
613 break;
614 case COMP_ZLIB_NORMAL:
615 av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
616 break;
617 default:
618 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
619 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
620 return 1;
621 }
622 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
623 }
624 #else
625 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
626 return 1;
627 #endif
628 break;
629 default:
630 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
631 return 1;
632 }
633
634 /* Allocate decompression buffer */
635 if (c->decomp_size) {
636 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
637 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
638 return 1;
639 }
640 }
641
642 /* Detect flags */
643 c->flags = *((char *)avctx->extradata + 6);
644 if (c->flags & FLAG_MULTITHREAD)
645 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
646 if (c->flags & FLAG_NULLFRAME)
647 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
648 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
649 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
650 if (c->flags & FLAGMASK_UNUSED)
651 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
652
653 /* If needed init zlib */
654 if (avctx->codec_id == CODEC_ID_ZLIB) {
655 #ifdef CONFIG_ZLIB
656 c->zstream.zalloc = Z_NULL;
657 c->zstream.zfree = Z_NULL;
658 c->zstream.opaque = Z_NULL;
659 zret = inflateInit(&(c->zstream));
660 if (zret != Z_OK) {
661 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
662 return 1;
663 }
664 #else
665 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
666 return 1;
667 #endif
668 }
669
670 avctx->pix_fmt = PIX_FMT_BGR24;
671
672 return 0;
673 }
674
675 /*
676 *
677 * Uninit lcl decoder
678 *
679 */
680 static int decode_end(AVCodecContext *avctx)
681 {
682 LclDecContext * const c = avctx->priv_data;
683
684 if (c->pic.data[0])
685 avctx->release_buffer(avctx, &c->pic);
686 #ifdef CONFIG_ZLIB
687 inflateEnd(&(c->zstream));
688 #endif
689
690 return 0;
691 }
692
693 #ifdef CONFIG_MSZH_DECODER
694 AVCodec mszh_decoder = {
695 "mszh",
696 CODEC_TYPE_VIDEO,
697 CODEC_ID_MSZH,
698 sizeof(LclDecContext),
699 decode_init,
700 NULL,
701 decode_end,
702 decode_frame,
703 CODEC_CAP_DR1,
704 };
705 #endif
706
707 #ifdef CONFIG_ZLIB_DECODER
708 AVCodec zlib_decoder = {
709 "zlib",
710 CODEC_TYPE_VIDEO,
711 CODEC_ID_ZLIB,
712 sizeof(LclDecContext),
713 decode_init,
714 NULL,
715 decode_end,
716 decode_frame,
717 CODEC_CAP_DR1,
718 };
719 #endif