Statistics
| Branch: | Revision:

ffmpeg / libavcodec / lcldec.c @ f005808b

History | View | Annotate | Download (22.4 KB)

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 libavcodec/lcldec.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 "get_bits.h"
46
#include "lcl.h"
47

    
48
#if CONFIG_ZLIB_DECODER
49
#include <zlib.h>
50
#endif
51

    
52
/*
53
 * Decoder context
54
 */
55
typedef struct LclDecContext {
56
    AVFrame pic;
57

    
58
    // Image type
59
    int imgtype;
60
    // Compression type
61
    int compression;
62
    // Flags
63
    int flags;
64
    // Decompressed data size
65
    unsigned int decomp_size;
66
    // Decompression buffer
67
    unsigned char* decomp_buf;
68
#if CONFIG_ZLIB_DECODER
69
    z_stream zstream;
70
#endif
71
} LclDecContext;
72

    
73

    
74
static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
75
{
76
    unsigned char *destptr_bak = destptr;
77
    unsigned char *destptr_end = destptr + destsize;
78
    unsigned char mask = 0;
79
    unsigned char maskbit = 0;
80
    unsigned int ofs, cnt;
81

    
82
    while (srclen > 0 && destptr < destptr_end) {
83
        if (maskbit == 0) {
84
            mask = *srcptr++;
85
            maskbit = 8;
86
            srclen--;
87
            continue;
88
        }
89
        if ((mask & (1 << (--maskbit))) == 0) {
90
            if (destptr + 4 > destptr_end)
91
                break;
92
            AV_WN32(destptr, AV_RN32(srcptr));
93
            srclen -= 4;
94
            destptr += 4;
95
            srcptr += 4;
96
        } else {
97
            ofs = *srcptr++;
98
            cnt = *srcptr++;
99
            ofs += cnt * 256;
100
            cnt = ((cnt >> 3) & 0x1f) + 1;
101
            ofs &= 0x7ff;
102
            srclen -= 2;
103
            cnt *= 4;
104
            if (destptr + cnt > destptr_end) {
105
                cnt =  destptr_end - destptr;
106
            }
107
            for (; cnt > 0; cnt--) {
108
                *destptr = *(destptr - ofs);
109
                destptr++;
110
            }
111
        }
112
    }
113

    
114
    return destptr - destptr_bak;
115
}
116

    
117

    
118

    
119
/*
120
 *
121
 * Decode a frame
122
 *
123
 */
124
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
125
{
126
    const uint8_t *buf = avpkt->data;
127
    int buf_size = avpkt->size;
128
    LclDecContext * const c = avctx->priv_data;
129
    unsigned char *encoded = (unsigned char *)buf;
130
    unsigned int pixel_ptr;
131
    int row, col;
132
    unsigned char *outptr;
133
    uint8_t *y_out, *u_out, *v_out;
134
    unsigned int width = avctx->width; // Real image width
135
    unsigned int height = avctx->height; // Real image height
136
    unsigned int mszh_dlen;
137
    unsigned char yq, y1q, uq, vq;
138
    int uqvq;
139
    unsigned int mthread_inlen, mthread_outlen;
140
#if CONFIG_ZLIB_DECODER
141
    int zret; // Zlib return code
142
#endif
143
    unsigned int len = buf_size;
144

    
145
    if(c->pic.data[0])
146
        avctx->release_buffer(avctx, &c->pic);
147

    
148
    c->pic.reference = 0;
149
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
150
    if(avctx->get_buffer(avctx, &c->pic) < 0){
151
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
152
        return -1;
153
    }
154

    
155
    outptr = c->pic.data[0]; // Output image pointer
156

    
157
    /* Decompress frame */
158
    switch (avctx->codec_id) {
159
    case CODEC_ID_MSZH:
160
        switch (c->compression) {
161
        case COMP_MSZH:
162
            if (c->flags & FLAG_MULTITHREAD) {
163
                mthread_inlen = *(unsigned int*)encoded;
164
                mthread_outlen = *(unsigned int*)(encoded+4);
165
                if (mthread_outlen > c->decomp_size) // this should not happen
166
                    mthread_outlen = c->decomp_size;
167
                mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
168
                if (mthread_outlen != mszh_dlen) {
169
                    av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
170
                           mthread_outlen, mszh_dlen);
171
                    return -1;
172
                }
173
                mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
174
                                        c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
175
                if (mthread_outlen != mszh_dlen) {
176
                    av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
177
                           mthread_outlen, mszh_dlen);
178
                    return -1;
179
                }
180
                encoded = c->decomp_buf;
181
                len = c->decomp_size;
182
            } else {
183
                mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
184
                if (c->decomp_size != mszh_dlen) {
185
                    av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
186
                           c->decomp_size, mszh_dlen);
187
                    return -1;
188
                }
189
                encoded = c->decomp_buf;
190
                len = mszh_dlen;
191
            }
192
            break;
193
        case COMP_MSZH_NOCOMP:
194
            break;
195
        default:
196
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
197
            return -1;
198
        }
199
        break;
200
#if CONFIG_ZLIB_DECODER
201
    case CODEC_ID_ZLIB:
202
        /* Using the original dll with normal compression (-1) and RGB format
203
         * gives a file with ZLIB fourcc, but frame is really uncompressed.
204
         * To be sure that's true check also frame size */
205
        if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
206
            len == width * height * 3)
207
            break;
208
        zret = inflateReset(&c->zstream);
209
        if (zret != Z_OK) {
210
            av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
211
            return -1;
212
        }
213
        if (c->flags & FLAG_MULTITHREAD) {
214
            mthread_inlen = *(unsigned int*)encoded;
215
            mthread_outlen = *(unsigned int*)(encoded+4);
216
            if (mthread_outlen > c->decomp_size)
217
                mthread_outlen = c->decomp_size;
218
            c->zstream.next_in = encoded + 8;
219
            c->zstream.avail_in = mthread_inlen;
220
            c->zstream.next_out = c->decomp_buf;
221
            c->zstream.avail_out = c->decomp_size;
222
            zret = inflate(&c->zstream, Z_FINISH);
223
            if (zret != Z_OK && zret != Z_STREAM_END) {
224
                av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
225
                return -1;
226
            }
227
            if (mthread_outlen != (unsigned int)c->zstream.total_out) {
228
                av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
229
                       mthread_outlen, c->zstream.total_out);
230
                return -1;
231
            }
232
            zret = inflateReset(&c->zstream);
233
            if (zret != Z_OK) {
234
                av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
235
                return -1;
236
            }
237
            c->zstream.next_in = encoded + 8 + mthread_inlen;
238
            c->zstream.avail_in = len - mthread_inlen;
239
            c->zstream.next_out = c->decomp_buf + mthread_outlen;
240
            c->zstream.avail_out = c->decomp_size - mthread_outlen;
241
            zret = inflate(&c->zstream, Z_FINISH);
242
            if (zret != Z_OK && zret != Z_STREAM_END) {
243
                av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
244
                return -1;
245
            }
246
            if (mthread_outlen != (unsigned int)c->zstream.total_out) {
247
                av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
248
                       mthread_outlen, c->zstream.total_out);
249
                return -1;
250
            }
251
        } else {
252
            c->zstream.next_in = encoded;
253
            c->zstream.avail_in = len;
254
            c->zstream.next_out = c->decomp_buf;
255
            c->zstream.avail_out = c->decomp_size;
256
            zret = inflate(&c->zstream, Z_FINISH);
257
            if (zret != Z_OK && zret != Z_STREAM_END) {
258
                av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
259
                return -1;
260
            }
261
            if (c->decomp_size != (unsigned int)c->zstream.total_out) {
262
                av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
263
                       c->decomp_size, c->zstream.total_out);
264
                return -1;
265
            }
266
        }
267
        encoded = c->decomp_buf;
268
        len = c->decomp_size;
269
        break;
270
#endif
271
    default:
272
        av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
273
        return -1;
274
    }
275

    
276

    
277
    /* Apply PNG filter */
278
    if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
279
        switch (c->imgtype) {
280
        case IMGTYPE_YUV111:
281
        case IMGTYPE_RGB24:
282
            for (row = 0; row < height; row++) {
283
                pixel_ptr = row * width * 3;
284
                yq = encoded[pixel_ptr++];
285
                uqvq = AV_RL16(encoded+pixel_ptr);
286
                pixel_ptr += 2;
287
                for (col = 1; col < width; col++) {
288
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
289
                    uqvq -= AV_RL16(encoded+pixel_ptr+1);
290
                    AV_WL16(encoded+pixel_ptr+1, uqvq);
291
                    pixel_ptr += 3;
292
                }
293
            }
294
            break;
295
        case IMGTYPE_YUV422:
296
            for (row = 0; row < height; row++) {
297
                pixel_ptr = row * width * 2;
298
                yq = uq = vq =0;
299
                for (col = 0; col < width/4; col++) {
300
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
301
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
302
                    encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
303
                    encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
304
                    encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
305
                    encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
306
                    encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
307
                    encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
308
                    pixel_ptr += 8;
309
                }
310
            }
311
            break;
312
        case IMGTYPE_YUV411:
313
            for (row = 0; row < height; row++) {
314
                pixel_ptr = row * width / 2 * 3;
315
                yq = uq = vq =0;
316
                for (col = 0; col < width/4; col++) {
317
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
318
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
319
                    encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
320
                    encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
321
                    encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
322
                    encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
323
                    pixel_ptr += 6;
324
                }
325
            }
326
            break;
327
        case IMGTYPE_YUV211:
328
            for (row = 0; row < height; row++) {
329
                pixel_ptr = row * width * 2;
330
                yq = uq = vq =0;
331
                for (col = 0; col < width/2; col++) {
332
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
333
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
334
                    encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
335
                    encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
336
                    pixel_ptr += 4;
337
                }
338
            }
339
            break;
340
        case IMGTYPE_YUV420:
341
            for (row = 0; row < height/2; row++) {
342
                pixel_ptr = row * width * 3;
343
                yq = y1q = uq = vq =0;
344
                for (col = 0; col < width/2; col++) {
345
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
346
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
347
                    encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
348
                    encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
349
                    encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
350
                    encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
351
                    pixel_ptr += 6;
352
                }
353
            }
354
            break;
355
        default:
356
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
357
            return -1;
358
        }
359
    }
360

    
361
    /* Convert colorspace */
362
    y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
363
    u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
364
    v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
365
    switch (c->imgtype) {
366
    case IMGTYPE_YUV111:
367
        for (row = 0; row < height; row++) {
368
            for (col = 0; col < width; col++) {
369
                y_out[col] = *encoded++;
370
                u_out[col] = *encoded++ + 128;
371
                v_out[col] = *encoded++ + 128;
372
            }
373
            y_out -= c->pic.linesize[0];
374
            u_out -= c->pic.linesize[1];
375
            v_out -= c->pic.linesize[2];
376
        }
377
        break;
378
    case IMGTYPE_YUV422:
379
        for (row = 0; row < height; row++) {
380
            for (col = 0; col < width - 3; col += 4) {
381
                memcpy(y_out + col, encoded, 4);
382
                encoded += 4;
383
                u_out[ col >> 1     ] = *encoded++ + 128;
384
                u_out[(col >> 1) + 1] = *encoded++ + 128;
385
                v_out[ col >> 1     ] = *encoded++ + 128;
386
                v_out[(col >> 1) + 1] = *encoded++ + 128;
387
            }
388
            y_out -= c->pic.linesize[0];
389
            u_out -= c->pic.linesize[1];
390
            v_out -= c->pic.linesize[2];
391
        }
392
        break;
393
    case IMGTYPE_RGB24:
394
        for (row = height - 1; row >= 0; row--) {
395
            pixel_ptr = row * c->pic.linesize[0];
396
            memcpy(outptr + pixel_ptr, encoded, 3 * width);
397
            encoded += 3 * width;
398
        }
399
        break;
400
    case IMGTYPE_YUV411:
401
        for (row = 0; row < height; row++) {
402
            for (col = 0; col < width - 3; col += 4) {
403
                memcpy(y_out + col, encoded, 4);
404
                encoded += 4;
405
                u_out[col >> 2] = *encoded++ + 128;
406
                v_out[col >> 2] = *encoded++ + 128;
407
            }
408
            y_out -= c->pic.linesize[0];
409
            u_out -= c->pic.linesize[1];
410
            v_out -= c->pic.linesize[2];
411
        }
412
        break;
413
    case IMGTYPE_YUV211:
414
        for (row = 0; row < height; row++) {
415
            for (col = 0; col < width - 1; col += 2) {
416
                memcpy(y_out + col, encoded, 2);
417
                encoded += 2;
418
                u_out[col >> 1] = *encoded++ + 128;
419
                v_out[col >> 1] = *encoded++ + 128;
420
            }
421
            y_out -= c->pic.linesize[0];
422
            u_out -= c->pic.linesize[1];
423
            v_out -= c->pic.linesize[2];
424
        }
425
        break;
426
    case IMGTYPE_YUV420:
427
        u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
428
        v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
429
        for (row = 0; row < height - 1; row += 2) {
430
            for (col = 0; col < width - 1; col += 2) {
431
                memcpy(y_out + col, encoded, 2);
432
                encoded += 2;
433
                memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
434
                encoded += 2;
435
                u_out[col >> 1] = *encoded++ + 128;
436
                v_out[col >> 1] = *encoded++ + 128;
437
            }
438
            y_out -= c->pic.linesize[0] << 1;
439
            u_out -= c->pic.linesize[1];
440
            v_out -= c->pic.linesize[2];
441
        }
442
        break;
443
    default:
444
        av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
445
        return -1;
446
    }
447

    
448
    *data_size = sizeof(AVFrame);
449
    *(AVFrame*)data = c->pic;
450

    
451
    /* always report that the buffer was completely consumed */
452
    return buf_size;
453
}
454

    
455
/*
456
 *
457
 * Init lcl decoder
458
 *
459
 */
460
static av_cold int decode_init(AVCodecContext *avctx)
461
{
462
    LclDecContext * const c = avctx->priv_data;
463
    unsigned int basesize = avctx->width * avctx->height;
464
    unsigned int max_basesize = FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4);
465
    unsigned int max_decomp_size;
466

    
467
    c->pic.data[0] = NULL;
468

    
469
#if CONFIG_ZLIB_DECODER
470
    // Needed if zlib unused or init aborted before inflateInit
471
    memset(&c->zstream, 0, sizeof(z_stream));
472
#endif
473

    
474
    if (avctx->extradata_size < 8) {
475
        av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
476
        return 1;
477
    }
478

    
479
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
480
        return 1;
481
    }
482

    
483
    /* Check codec type */
484
    if ((avctx->codec_id == CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
485
        (avctx->codec_id == CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
486
        av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
487
    }
488

    
489
    /* Detect image type */
490
    switch (c->imgtype = avctx->extradata[4]) {
491
    case IMGTYPE_YUV111:
492
        c->decomp_size = basesize * 3;
493
        max_decomp_size = max_basesize * 3;
494
        avctx->pix_fmt = PIX_FMT_YUV444P;
495
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
496
        break;
497
    case IMGTYPE_YUV422:
498
        c->decomp_size = basesize * 2;
499
        max_decomp_size = max_basesize * 2;
500
        avctx->pix_fmt = PIX_FMT_YUV422P;
501
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
502
        break;
503
    case IMGTYPE_RGB24:
504
        c->decomp_size = basesize * 3;
505
        max_decomp_size = max_basesize * 3;
506
        avctx->pix_fmt = PIX_FMT_BGR24;
507
        av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
508
        break;
509
    case IMGTYPE_YUV411:
510
        c->decomp_size = basesize / 2 * 3;
511
        max_decomp_size = max_basesize / 2 * 3;
512
        avctx->pix_fmt = PIX_FMT_YUV411P;
513
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
514
        break;
515
    case IMGTYPE_YUV211:
516
        c->decomp_size = basesize * 2;
517
        max_decomp_size = max_basesize * 2;
518
        avctx->pix_fmt = PIX_FMT_YUV422P;
519
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
520
        break;
521
    case IMGTYPE_YUV420:
522
        c->decomp_size = basesize / 2 * 3;
523
        max_decomp_size = max_basesize / 2 * 3;
524
        avctx->pix_fmt = PIX_FMT_YUV420P;
525
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
526
        break;
527
    default:
528
        av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
529
        return 1;
530
    }
531

    
532
    /* Detect compression method */
533
    c->compression = avctx->extradata[5];
534
    switch (avctx->codec_id) {
535
    case CODEC_ID_MSZH:
536
        switch (c->compression) {
537
        case COMP_MSZH:
538
            av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
539
            break;
540
        case COMP_MSZH_NOCOMP:
541
            c->decomp_size = 0;
542
            av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
543
            break;
544
        default:
545
            av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
546
            return 1;
547
        }
548
        break;
549
#if CONFIG_ZLIB_DECODER
550
    case CODEC_ID_ZLIB:
551
        switch (c->compression) {
552
        case COMP_ZLIB_HISPEED:
553
            av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
554
            break;
555
        case COMP_ZLIB_HICOMP:
556
            av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
557
            break;
558
        case COMP_ZLIB_NORMAL:
559
            av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
560
            break;
561
        default:
562
            if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
563
                av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
564
                return 1;
565
            }
566
            av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
567
        }
568
        break;
569
#endif
570
    default:
571
        av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
572
        return 1;
573
    }
574

    
575
    /* Allocate decompression buffer */
576
    if (c->decomp_size) {
577
        if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
578
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
579
            return 1;
580
        }
581
    }
582

    
583
    /* Detect flags */
584
    c->flags = avctx->extradata[6];
585
    if (c->flags & FLAG_MULTITHREAD)
586
        av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
587
    if (c->flags & FLAG_NULLFRAME)
588
        av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
589
    if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
590
        av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
591
    if (c->flags & FLAGMASK_UNUSED)
592
        av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
593

    
594
    /* If needed init zlib */
595
#if CONFIG_ZLIB_DECODER
596
    if (avctx->codec_id == CODEC_ID_ZLIB) {
597
        int zret;
598
        c->zstream.zalloc = Z_NULL;
599
        c->zstream.zfree = Z_NULL;
600
        c->zstream.opaque = Z_NULL;
601
        zret = inflateInit(&c->zstream);
602
        if (zret != Z_OK) {
603
            av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
604
            return 1;
605
        }
606
    }
607
#endif
608

    
609
    return 0;
610
}
611

    
612
/*
613
 *
614
 * Uninit lcl decoder
615
 *
616
 */
617
static av_cold int decode_end(AVCodecContext *avctx)
618
{
619
    LclDecContext * const c = avctx->priv_data;
620

    
621
    if (c->pic.data[0])
622
        avctx->release_buffer(avctx, &c->pic);
623
#if CONFIG_ZLIB_DECODER
624
    inflateEnd(&c->zstream);
625
#endif
626

    
627
    return 0;
628
}
629

    
630
#if CONFIG_MSZH_DECODER
631
AVCodec mszh_decoder = {
632
    "mszh",
633
    CODEC_TYPE_VIDEO,
634
    CODEC_ID_MSZH,
635
    sizeof(LclDecContext),
636
    decode_init,
637
    NULL,
638
    decode_end,
639
    decode_frame,
640
    CODEC_CAP_DR1,
641
    .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
642
};
643
#endif
644

    
645
#if CONFIG_ZLIB_DECODER
646
AVCodec zlib_decoder = {
647
    "zlib",
648
    CODEC_TYPE_VIDEO,
649
    CODEC_ID_ZLIB,
650
    sizeof(LclDecContext),
651
    decode_init,
652
    NULL,
653
    decode_end,
654
    decode_frame,
655
    CODEC_CAP_DR1,
656
    .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
657
};
658
#endif