2
* LCL (LossLess Codec Library) Codec
3
* Copyright (c) 2002-2004 Roberto Togni
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
* LCL (LossLess Codec Library) Video Codec
26
* Decoder for MSZH and ZLIB codecs
27
* Experimental encoder for ZLIB RGB24
32
* Ver2.23 By Kenji Oshima 2000.09.20
33
* avimszh.dll, avizlib.dll
35
* A description of the decoding algorithm can be found here:
36
* http://www.pcisys.net/~melanson/codecs
38
* Supports: BGR24 (RGB 24bpp)
46
#include "bitstream.h"
57
#define IMGTYPE_YUV111 0
58
#define IMGTYPE_YUV422 1
59
#define IMGTYPE_RGB24 2
60
#define IMGTYPE_YUV411 3
61
#define IMGTYPE_YUV211 4
62
#define IMGTYPE_YUV420 5
65
#define COMP_MSZH_NOCOMP 1
66
#define COMP_ZLIB_HISPEED 1
67
#define COMP_ZLIB_HICOMP 9
68
#define COMP_ZLIB_NORMAL -1
70
#define FLAG_MULTITHREAD 1
71
#define FLAG_NULLFRAME 2
72
#define FLAG_PNGFILTER 4
73
#define FLAGMASK_UNUSED 0xf8
78
#define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
79
#define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
84
typedef struct LclContext {
86
AVCodecContext *avctx;
96
// Decompressed data size
97
unsigned int decomp_size;
98
// Decompression buffer
99
unsigned char* decomp_buf;
100
// Maximum compressed data size
101
unsigned int max_comp_size;
102
// Compression buffer
103
unsigned char* comp_buf;
115
static inline unsigned char fix (int pix14)
119
tmp = (pix14 + 0x80000) >> 20;
129
static inline unsigned char get_b (unsigned char yq, signed char bq)
131
return fix((yq << 20) + bq * 1858076);
136
static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
138
return fix((yq << 20) - bq * 360857 - rq * 748830);
143
static inline unsigned char get_r (unsigned char yq, signed char rq)
145
return fix((yq << 20) + rq * 1470103);
150
static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
152
unsigned char *destptr_bak = destptr;
153
unsigned char *destptr_end = destptr + destsize;
154
unsigned char mask = 0;
155
unsigned char maskbit = 0;
156
unsigned int ofs, cnt;
158
while ((srclen > 0) && (destptr < destptr_end)) {
165
if ((mask & (1 << (--maskbit))) == 0) {
166
if (destptr + 4 > destptr_end)
168
*(int*)destptr = *(int*)srcptr;
176
cnt = ((cnt >> 3) & 0x1f) + 1;
180
if (destptr + cnt > destptr_end) {
181
cnt = destptr_end - destptr;
183
for (; cnt > 0; cnt--) {
184
*(destptr) = *(destptr - ofs);
190
return (destptr - destptr_bak);
195
#ifdef CONFIG_DECODERS
201
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
203
LclContext * const c = (LclContext *)avctx->priv_data;
204
unsigned char *encoded = (unsigned char *)buf;
205
unsigned int pixel_ptr;
207
unsigned char *outptr;
208
unsigned int width = avctx->width; // Real image width
209
unsigned int height = avctx->height; // Real image height
210
unsigned int mszh_dlen;
211
unsigned char yq, y1q, uq, vq;
213
unsigned int mthread_inlen, mthread_outlen;
215
int zret; // Zlib return code
217
unsigned int len = buf_size;
220
avctx->release_buffer(avctx, &c->pic);
222
c->pic.reference = 0;
223
c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
224
if(avctx->get_buffer(avctx, &c->pic) < 0){
225
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
229
outptr = c->pic.data[0]; // Output image pointer
231
/* Decompress frame */
232
switch (avctx->codec_id) {
234
switch (c->compression) {
236
if (c->flags & FLAG_MULTITHREAD) {
237
mthread_inlen = *((unsigned int*)encoded);
238
mthread_outlen = *((unsigned int*)(encoded+4));
239
if (mthread_outlen > c->decomp_size) // this should not happen
240
mthread_outlen = c->decomp_size;
241
mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
242
if (mthread_outlen != mszh_dlen) {
243
av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
244
mthread_outlen, mszh_dlen);
247
mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
248
c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
249
if (mthread_outlen != mszh_dlen) {
250
av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
251
mthread_outlen, mszh_dlen);
254
encoded = c->decomp_buf;
255
len = c->decomp_size;
257
mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
258
if (c->decomp_size != mszh_dlen) {
259
av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
260
c->decomp_size, mszh_dlen);
263
encoded = c->decomp_buf;
267
case COMP_MSZH_NOCOMP:
270
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
276
/* Using the original dll with normal compression (-1) and RGB format
277
* gives a file with ZLIB fourcc, but frame is really uncompressed.
278
* To be sure that's true check also frame size */
279
if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
280
(len == width * height * 3))
282
zret = inflateReset(&(c->zstream));
284
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
287
if (c->flags & FLAG_MULTITHREAD) {
288
mthread_inlen = *((unsigned int*)encoded);
289
mthread_outlen = *((unsigned int*)(encoded+4));
290
if (mthread_outlen > c->decomp_size)
291
mthread_outlen = c->decomp_size;
292
c->zstream.next_in = encoded + 8;
293
c->zstream.avail_in = mthread_inlen;
294
c->zstream.next_out = c->decomp_buf;
295
c->zstream.avail_out = c->decomp_size;
296
zret = inflate(&(c->zstream), Z_FINISH);
297
if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
298
av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
301
if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
302
av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
303
mthread_outlen, c->zstream.total_out);
306
zret = inflateReset(&(c->zstream));
308
av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
311
c->zstream.next_in = encoded + 8 + mthread_inlen;
312
c->zstream.avail_in = len - mthread_inlen;
313
c->zstream.next_out = c->decomp_buf + mthread_outlen;
314
c->zstream.avail_out = c->decomp_size - mthread_outlen;
315
zret = inflate(&(c->zstream), Z_FINISH);
316
if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
317
av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
320
if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
321
av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
322
mthread_outlen, c->zstream.total_out);
326
c->zstream.next_in = encoded;
327
c->zstream.avail_in = len;
328
c->zstream.next_out = c->decomp_buf;
329
c->zstream.avail_out = c->decomp_size;
330
zret = inflate(&(c->zstream), Z_FINISH);
331
if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
332
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
335
if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
336
av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
337
c->decomp_size, c->zstream.total_out);
341
encoded = c->decomp_buf;
342
len = c->decomp_size;;
344
av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
349
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
354
/* Apply PNG filter */
355
if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
356
switch (c->imgtype) {
359
for (row = 0; row < height; row++) {
360
pixel_ptr = row * width * 3;
361
yq = encoded[pixel_ptr++];
362
uqvq = encoded[pixel_ptr++];
363
uqvq+=(encoded[pixel_ptr++] << 8);
364
for (col = 1; col < width; col++) {
365
encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
366
uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8));
367
encoded[pixel_ptr+1] = (uqvq) & 0xff;
368
encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff;
374
for (row = 0; row < height; row++) {
375
pixel_ptr = row * width * 2;
377
for (col = 0; col < width/4; col++) {
378
encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
379
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
380
encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
381
encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
382
encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
383
encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
384
encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
385
encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
391
for (row = 0; row < height; row++) {
392
pixel_ptr = row * width / 2 * 3;
394
for (col = 0; col < width/4; col++) {
395
encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
396
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
397
encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
398
encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
399
encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
400
encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
406
for (row = 0; row < height; row++) {
407
pixel_ptr = row * width * 2;
409
for (col = 0; col < width/2; col++) {
410
encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
411
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
412
encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
413
encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
419
for (row = 0; row < height/2; row++) {
420
pixel_ptr = row * width * 3;
421
yq = y1q = uq = vq =0;
422
for (col = 0; col < width/2; col++) {
423
encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
424
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
425
encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
426
encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
427
encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
428
encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
434
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
439
/* Convert colorspace */
440
switch (c->imgtype) {
442
for (row = height - 1; row >= 0; row--) {
443
pixel_ptr = row * c->pic.linesize[0];
444
for (col = 0; col < width; col++) {
445
outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
446
outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
447
outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
453
for (row = height - 1; row >= 0; row--) {
454
pixel_ptr = row * c->pic.linesize[0];
455
for (col = 0; col < width/4; col++) {
456
outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
457
outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
458
outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
459
outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
460
outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
461
outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
462
outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
463
outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
464
outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
465
outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
466
outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
467
outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
473
for (row = height - 1; row >= 0; row--) {
474
pixel_ptr = row * c->pic.linesize[0];
475
for (col = 0; col < width; col++) {
476
outptr[pixel_ptr++] = encoded[0];
477
outptr[pixel_ptr++] = encoded[1];
478
outptr[pixel_ptr++] = encoded[2];
484
for (row = height - 1; row >= 0; row--) {
485
pixel_ptr = row * c->pic.linesize[0];
486
for (col = 0; col < width/4; col++) {
487
outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
488
outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
489
outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
490
outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
491
outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
492
outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
493
outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
494
outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
495
outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
496
outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
497
outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
498
outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
504
for (row = height - 1; row >= 0; row--) {
505
pixel_ptr = row * c->pic.linesize[0];
506
for (col = 0; col < width/2; col++) {
507
outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
508
outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
509
outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
510
outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
511
outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
512
outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
518
for (row = height / 2 - 1; row >= 0; row--) {
519
pixel_ptr = 2 * row * c->pic.linesize[0];
520
for (col = 0; col < width/2; col++) {
521
outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
522
outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
523
outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
524
outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
525
outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
526
outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
527
outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
528
outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
529
outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
530
outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
531
outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
532
outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
539
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
543
*data_size = sizeof(AVFrame);
544
*(AVFrame*)data = c->pic;
546
/* always report that the buffer was completely consumed */
551
#ifdef CONFIG_ENCODERS
557
static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
558
LclContext *c = avctx->priv_data;
559
AVFrame *pict = data;
560
AVFrame * const p = &c->pic;
562
int zret; // Zlib return code
565
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
569
init_put_bits(&c->pb, buf, buf_size);
572
p->pict_type= FF_I_TYPE;
575
if(avctx->pix_fmt != PIX_FMT_BGR24){
576
av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
580
zret = deflateReset(&(c->zstream));
582
av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
585
c->zstream.next_out = c->comp_buf;
586
c->zstream.avail_out = c->max_comp_size;
588
for(i = avctx->height - 1; i >= 0; i--) {
589
c->zstream.next_in = p->data[0]+p->linesize[0]*i;
590
c->zstream.avail_in = avctx->width*3;
591
zret = deflate(&(c->zstream), Z_NO_FLUSH);
593
av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
597
zret = deflate(&(c->zstream), Z_FINISH);
598
if (zret != Z_STREAM_END) {
599
av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
603
for (i = 0; i < c->zstream.total_out; i++)
604
put_bits(&c->pb, 8, c->comp_buf[i]);
605
flush_put_bits(&c->pb);
607
return c->zstream.total_out;
610
#endif /* CONFIG_ENCODERS */
612
#ifdef CONFIG_DECODERS
618
static int decode_init(AVCodecContext *avctx)
620
LclContext * const c = (LclContext *)avctx->priv_data;
621
unsigned int basesize = avctx->width * avctx->height;
622
unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
623
unsigned int max_decomp_size;
624
int zret; // Zlib return code
627
avctx->has_b_frames = 0;
629
c->pic.data[0] = NULL;
632
// Needed if zlib unused or init aborted before inflateInit
633
memset(&(c->zstream), 0, sizeof(z_stream));
636
if (avctx->extradata_size < 8) {
637
av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
641
if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
645
/* Check codec type */
646
if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
647
((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
648
av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
651
/* Detect image type */
652
switch (c->imgtype = *((char *)avctx->extradata + 4)) {
654
c->decomp_size = basesize * 3;
655
max_decomp_size = max_basesize * 3;
656
av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
659
c->decomp_size = basesize * 2;
660
max_decomp_size = max_basesize * 2;
661
av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
664
c->decomp_size = basesize * 3;
665
max_decomp_size = max_basesize * 3;
666
av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
669
c->decomp_size = basesize / 2 * 3;
670
max_decomp_size = max_basesize / 2 * 3;
671
av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
674
c->decomp_size = basesize * 2;
675
max_decomp_size = max_basesize * 2;
676
av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
679
c->decomp_size = basesize / 2 * 3;
680
max_decomp_size = max_basesize / 2 * 3;
681
av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
684
av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
688
/* Detect compression method */
689
c->compression = *((char *)avctx->extradata + 5);
690
switch (avctx->codec_id) {
692
switch (c->compression) {
694
av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
696
case COMP_MSZH_NOCOMP:
698
av_log(avctx, AV_LOG_INFO, "No compression.\n");
701
av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
707
switch (c->compression) {
708
case COMP_ZLIB_HISPEED:
709
av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
711
case COMP_ZLIB_HICOMP:
712
av_log(avctx, AV_LOG_INFO, "High compression.\n");
714
case COMP_ZLIB_NORMAL:
715
av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
718
if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
719
av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
722
av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
725
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
730
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
734
/* Allocate decompression buffer */
735
if (c->decomp_size) {
736
if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
737
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
743
c->flags = *((char *)avctx->extradata + 6);
744
if (c->flags & FLAG_MULTITHREAD)
745
av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
746
if (c->flags & FLAG_NULLFRAME)
747
av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
748
if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
749
av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
750
if (c->flags & FLAGMASK_UNUSED)
751
av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
753
/* If needed init zlib */
754
if (avctx->codec_id == CODEC_ID_ZLIB) {
756
c->zstream.zalloc = Z_NULL;
757
c->zstream.zfree = Z_NULL;
758
c->zstream.opaque = Z_NULL;
759
zret = inflateInit(&(c->zstream));
761
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
765
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
770
avctx->pix_fmt = PIX_FMT_BGR24;
774
#endif /* CONFIG_DECODERS */
776
#ifdef CONFIG_ENCODERS
782
static int encode_init(AVCodecContext *avctx)
784
LclContext *c = avctx->priv_data;
785
int zret; // Zlib return code
788
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
794
assert(avctx->width && avctx->height);
796
avctx->extradata= av_mallocz(8);
797
avctx->coded_frame= &c->pic;
799
// Will be user settable someday
803
switch(avctx->pix_fmt){
805
c->imgtype = IMGTYPE_RGB24;
806
c->decomp_size = avctx->width * avctx->height * 3;
807
avctx->bits_per_sample= 24;
810
av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
814
((uint8_t*)avctx->extradata)[0]= 4;
815
((uint8_t*)avctx->extradata)[1]= 0;
816
((uint8_t*)avctx->extradata)[2]= 0;
817
((uint8_t*)avctx->extradata)[3]= 0;
818
((uint8_t*)avctx->extradata)[4]= c->imgtype;
819
((uint8_t*)avctx->extradata)[5]= c->compression;
820
((uint8_t*)avctx->extradata)[6]= c->flags;
821
((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
822
c->avctx->extradata_size= 8;
824
c->zstream.zalloc = Z_NULL;
825
c->zstream.zfree = Z_NULL;
826
c->zstream.opaque = Z_NULL;
827
zret = deflateInit(&(c->zstream), c->compression);
829
av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
833
/* Conservative upper bound taken from zlib v1.2.1 source */
834
c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
835
((c->decomp_size + 63) >> 6) + 11;
836
if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
837
av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
844
#endif /* CONFIG_ENCODERS */
848
#ifdef CONFIG_DECODERS
854
static int decode_end(AVCodecContext *avctx)
856
LclContext * const c = (LclContext *)avctx->priv_data;
859
avctx->release_buffer(avctx, &c->pic);
861
inflateEnd(&(c->zstream));
868
#ifdef CONFIG_ENCODERS
874
static int encode_end(AVCodecContext *avctx)
876
LclContext *c = avctx->priv_data;
878
av_freep(&avctx->extradata);
879
av_freep(&c->comp_buf);
881
deflateEnd(&(c->zstream));
888
#ifdef CONFIG_MSZH_DECODER
889
AVCodec mszh_decoder = {
902
#ifdef CONFIG_ZLIB_DECODER
903
AVCodec zlib_decoder = {
916
#ifdef CONFIG_ENCODERS
918
AVCodec zlib_encoder = {
928
#endif //CONFIG_ENCODERS