Statistics
| Branch: | Revision:

ffmpeg / libavcodec / lcldec.c @ e0b3af90

History | View | Annotate | Download (21.5 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 "bytestream.h"
46
#include "lcl.h"
47
#include "libavutil/lzo.h"
48

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

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

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

    
74

    
75
/**
76
 * \param srcptr compressed source buffer, must be padded with at least 4 extra bytes
77
 * \param destptr must be padded sufficiently for av_memcpy_backptr
78
 */
79
static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
80
{
81
    unsigned char *destptr_bak = destptr;
82
    unsigned char *destptr_end = destptr + destsize;
83
    const unsigned char *srcptr_end = srcptr + srclen;
84
    unsigned char mask = 0;
85
    unsigned char maskbit = 0;
86
    unsigned int ofs, cnt;
87

    
88
    while (srcptr < srcptr_end && destptr < destptr_end) {
89
        if (maskbit == 0) {
90
            mask = *srcptr++;
91
            maskbit = 8;
92
            continue;
93
        }
94
        if ((mask & (1 << (--maskbit))) == 0) {
95
            memcpy(destptr, srcptr, 4);
96
            destptr += 4;
97
            srcptr += 4;
98
        } else {
99
            ofs = bytestream_get_le16(&srcptr);
100
            cnt = (ofs >> 11) + 1;
101
            ofs &= 0x7ff;
102
            cnt *= 4;
103
            cnt = FFMIN(cnt, destptr_end - destptr);
104
            av_memcpy_backptr(destptr, ofs, cnt);
105
            destptr += cnt;
106
        }
107
    }
108

    
109
    return destptr - destptr_bak;
110
}
111

    
112

    
113
/**
114
 * \brief decompress a zlib-compressed data block into decomp_buf
115
 * \param src compressed input buffer
116
 * \param src_len data length in input buffer
117
 * \param offset offset in decomp_buf
118
 * \param expected expected decompressed length
119
 */
120
static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
121
{
122
    LclDecContext *c = avctx->priv_data;
123
    int zret = inflateReset(&c->zstream);
124
    if (zret != Z_OK) {
125
        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
126
        return -1;
127
    }
128
    c->zstream.next_in = src;
129
    c->zstream.avail_in = src_len;
130
    c->zstream.next_out = c->decomp_buf + offset;
131
    c->zstream.avail_out = c->decomp_size - offset;
132
    zret = inflate(&c->zstream, Z_FINISH);
133
    if (zret != Z_OK && zret != Z_STREAM_END) {
134
        av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
135
        return -1;
136
    }
137
    if (expected != (unsigned int)c->zstream.total_out) {
138
        av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
139
               expected, c->zstream.total_out);
140
        return -1;
141
    }
142
    return c->zstream.total_out;
143
}
144

    
145

    
146
/*
147
 *
148
 * Decode a frame
149
 *
150
 */
151
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
152
{
153
    const uint8_t *buf = avpkt->data;
154
    int buf_size = avpkt->size;
155
    LclDecContext * const c = avctx->priv_data;
156
    unsigned char *encoded = (unsigned char *)buf;
157
    unsigned int pixel_ptr;
158
    int row, col;
159
    unsigned char *outptr;
160
    uint8_t *y_out, *u_out, *v_out;
161
    unsigned int width = avctx->width; // Real image width
162
    unsigned int height = avctx->height; // Real image height
163
    unsigned int mszh_dlen;
164
    unsigned char yq, y1q, uq, vq;
165
    int uqvq;
166
    unsigned int mthread_inlen, mthread_outlen;
167
    unsigned int len = buf_size;
168

    
169
    if(c->pic.data[0])
170
        avctx->release_buffer(avctx, &c->pic);
171

    
172
    c->pic.reference = 0;
173
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
174
    if(avctx->get_buffer(avctx, &c->pic) < 0){
175
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
176
        return -1;
177
    }
178

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

    
181
    /* Decompress frame */
182
    switch (avctx->codec_id) {
183
    case CODEC_ID_MSZH:
184
        switch (c->compression) {
185
        case COMP_MSZH:
186
            if (c->flags & FLAG_MULTITHREAD) {
187
                mthread_inlen = AV_RL32(encoded);
188
                mthread_inlen = FFMIN(mthread_inlen, len - 8);
189
                mthread_outlen = AV_RL32(encoded+4);
190
                mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
191
                mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
192
                if (mthread_outlen != mszh_dlen) {
193
                    av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
194
                           mthread_outlen, mszh_dlen);
195
                    return -1;
196
                }
197
                mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
198
                                        c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
199
                if (mthread_outlen != mszh_dlen) {
200
                    av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
201
                           mthread_outlen, mszh_dlen);
202
                    return -1;
203
                }
204
                encoded = c->decomp_buf;
205
                len = c->decomp_size;
206
            } else {
207
                mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
208
                if (c->decomp_size != mszh_dlen) {
209
                    av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
210
                           c->decomp_size, mszh_dlen);
211
                    return -1;
212
                }
213
                encoded = c->decomp_buf;
214
                len = mszh_dlen;
215
            }
216
            break;
217
        case COMP_MSZH_NOCOMP:
218
            break;
219
        default:
220
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
221
            return -1;
222
        }
223
        break;
224
#if CONFIG_ZLIB_DECODER
225
    case CODEC_ID_ZLIB:
226
        /* Using the original dll with normal compression (-1) and RGB format
227
         * gives a file with ZLIB fourcc, but frame is really uncompressed.
228
         * To be sure that's true check also frame size */
229
        if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
230
            len == width * height * 3)
231
            break;
232
        if (c->flags & FLAG_MULTITHREAD) {
233
            int ret;
234
            mthread_inlen = AV_RL32(encoded);
235
            mthread_inlen = FFMIN(mthread_inlen, len - 8);
236
            mthread_outlen = AV_RL32(encoded+4);
237
            mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
238
            ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
239
            if (ret < 0) return ret;
240
            ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
241
                              mthread_outlen, mthread_outlen);
242
            if (ret < 0) return ret;
243
        } else {
244
            int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
245
            if (ret < 0) return ret;
246
        }
247
        encoded = c->decomp_buf;
248
        len = c->decomp_size;
249
        break;
250
#endif
251
    default:
252
        av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
253
        return -1;
254
    }
255

    
256

    
257
    /* Apply PNG filter */
258
    if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
259
        switch (c->imgtype) {
260
        case IMGTYPE_YUV111:
261
        case IMGTYPE_RGB24:
262
            for (row = 0; row < height; row++) {
263
                pixel_ptr = row * width * 3;
264
                yq = encoded[pixel_ptr++];
265
                uqvq = AV_RL16(encoded+pixel_ptr);
266
                pixel_ptr += 2;
267
                for (col = 1; col < width; col++) {
268
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
269
                    uqvq -= AV_RL16(encoded+pixel_ptr+1);
270
                    AV_WL16(encoded+pixel_ptr+1, uqvq);
271
                    pixel_ptr += 3;
272
                }
273
            }
274
            break;
275
        case IMGTYPE_YUV422:
276
            for (row = 0; row < height; row++) {
277
                pixel_ptr = row * width * 2;
278
                yq = uq = vq =0;
279
                for (col = 0; col < width/4; col++) {
280
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
281
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
282
                    encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
283
                    encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
284
                    encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
285
                    encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
286
                    encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
287
                    encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
288
                    pixel_ptr += 8;
289
                }
290
            }
291
            break;
292
        case IMGTYPE_YUV411:
293
            for (row = 0; row < height; row++) {
294
                pixel_ptr = row * width / 2 * 3;
295
                yq = uq = vq =0;
296
                for (col = 0; col < width/4; col++) {
297
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
298
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
299
                    encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
300
                    encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
301
                    encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
302
                    encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
303
                    pixel_ptr += 6;
304
                }
305
            }
306
            break;
307
        case IMGTYPE_YUV211:
308
            for (row = 0; row < height; row++) {
309
                pixel_ptr = row * width * 2;
310
                yq = uq = vq =0;
311
                for (col = 0; col < width/2; col++) {
312
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
313
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
314
                    encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
315
                    encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
316
                    pixel_ptr += 4;
317
                }
318
            }
319
            break;
320
        case IMGTYPE_YUV420:
321
            for (row = 0; row < height/2; row++) {
322
                pixel_ptr = row * width * 3;
323
                yq = y1q = uq = vq =0;
324
                for (col = 0; col < width/2; col++) {
325
                    encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
326
                    encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
327
                    encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
328
                    encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
329
                    encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
330
                    encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
331
                    pixel_ptr += 6;
332
                }
333
            }
334
            break;
335
        default:
336
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
337
            return -1;
338
        }
339
    }
340

    
341
    /* Convert colorspace */
342
    y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
343
    u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
344
    v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
345
    switch (c->imgtype) {
346
    case IMGTYPE_YUV111:
347
        for (row = 0; row < height; row++) {
348
            for (col = 0; col < width; col++) {
349
                y_out[col] = *encoded++;
350
                u_out[col] = *encoded++ + 128;
351
                v_out[col] = *encoded++ + 128;
352
            }
353
            y_out -= c->pic.linesize[0];
354
            u_out -= c->pic.linesize[1];
355
            v_out -= c->pic.linesize[2];
356
        }
357
        break;
358
    case IMGTYPE_YUV422:
359
        for (row = 0; row < height; row++) {
360
            for (col = 0; col < width - 3; col += 4) {
361
                memcpy(y_out + col, encoded, 4);
362
                encoded += 4;
363
                u_out[ col >> 1     ] = *encoded++ + 128;
364
                u_out[(col >> 1) + 1] = *encoded++ + 128;
365
                v_out[ col >> 1     ] = *encoded++ + 128;
366
                v_out[(col >> 1) + 1] = *encoded++ + 128;
367
            }
368
            y_out -= c->pic.linesize[0];
369
            u_out -= c->pic.linesize[1];
370
            v_out -= c->pic.linesize[2];
371
        }
372
        break;
373
    case IMGTYPE_RGB24:
374
        for (row = height - 1; row >= 0; row--) {
375
            pixel_ptr = row * c->pic.linesize[0];
376
            memcpy(outptr + pixel_ptr, encoded, 3 * width);
377
            encoded += 3 * width;
378
        }
379
        break;
380
    case IMGTYPE_YUV411:
381
        for (row = 0; row < height; row++) {
382
            for (col = 0; col < width - 3; col += 4) {
383
                memcpy(y_out + col, encoded, 4);
384
                encoded += 4;
385
                u_out[col >> 2] = *encoded++ + 128;
386
                v_out[col >> 2] = *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_YUV211:
394
        for (row = 0; row < height; row++) {
395
            for (col = 0; col < width - 1; col += 2) {
396
                memcpy(y_out + col, encoded, 2);
397
                encoded += 2;
398
                u_out[col >> 1] = *encoded++ + 128;
399
                v_out[col >> 1] = *encoded++ + 128;
400
            }
401
            y_out -= c->pic.linesize[0];
402
            u_out -= c->pic.linesize[1];
403
            v_out -= c->pic.linesize[2];
404
        }
405
        break;
406
    case IMGTYPE_YUV420:
407
        u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
408
        v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
409
        for (row = 0; row < height - 1; row += 2) {
410
            for (col = 0; col < width - 1; col += 2) {
411
                memcpy(y_out + col, encoded, 2);
412
                encoded += 2;
413
                memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
414
                encoded += 2;
415
                u_out[col >> 1] = *encoded++ + 128;
416
                v_out[col >> 1] = *encoded++ + 128;
417
            }
418
            y_out -= c->pic.linesize[0] << 1;
419
            u_out -= c->pic.linesize[1];
420
            v_out -= c->pic.linesize[2];
421
        }
422
        break;
423
    default:
424
        av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
425
        return -1;
426
    }
427

    
428
    *data_size = sizeof(AVFrame);
429
    *(AVFrame*)data = c->pic;
430

    
431
    /* always report that the buffer was completely consumed */
432
    return buf_size;
433
}
434

    
435
/*
436
 *
437
 * Init lcl decoder
438
 *
439
 */
440
static av_cold int decode_init(AVCodecContext *avctx)
441
{
442
    LclDecContext * const c = avctx->priv_data;
443
    unsigned int basesize = avctx->width * avctx->height;
444
    unsigned int max_basesize = FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4) + AV_LZO_OUTPUT_PADDING;
445
    unsigned int max_decomp_size;
446

    
447
    c->pic.data[0] = NULL;
448

    
449
#if CONFIG_ZLIB_DECODER
450
    // Needed if zlib unused or init aborted before inflateInit
451
    memset(&c->zstream, 0, sizeof(z_stream));
452
#endif
453

    
454
    if (avctx->extradata_size < 8) {
455
        av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
456
        return 1;
457
    }
458

    
459
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
460
        return 1;
461
    }
462

    
463
    /* Check codec type */
464
    if ((avctx->codec_id == CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
465
        (avctx->codec_id == CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
466
        av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
467
    }
468

    
469
    /* Detect image type */
470
    switch (c->imgtype = avctx->extradata[4]) {
471
    case IMGTYPE_YUV111:
472
        c->decomp_size = basesize * 3;
473
        max_decomp_size = max_basesize * 3;
474
        avctx->pix_fmt = PIX_FMT_YUV444P;
475
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
476
        break;
477
    case IMGTYPE_YUV422:
478
        c->decomp_size = basesize * 2;
479
        max_decomp_size = max_basesize * 2;
480
        avctx->pix_fmt = PIX_FMT_YUV422P;
481
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
482
        break;
483
    case IMGTYPE_RGB24:
484
        c->decomp_size = basesize * 3;
485
        max_decomp_size = max_basesize * 3;
486
        avctx->pix_fmt = PIX_FMT_BGR24;
487
        av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
488
        break;
489
    case IMGTYPE_YUV411:
490
        c->decomp_size = basesize / 2 * 3;
491
        max_decomp_size = max_basesize / 2 * 3;
492
        avctx->pix_fmt = PIX_FMT_YUV411P;
493
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
494
        break;
495
    case IMGTYPE_YUV211:
496
        c->decomp_size = basesize * 2;
497
        max_decomp_size = max_basesize * 2;
498
        avctx->pix_fmt = PIX_FMT_YUV422P;
499
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
500
        break;
501
    case IMGTYPE_YUV420:
502
        c->decomp_size = basesize / 2 * 3;
503
        max_decomp_size = max_basesize / 2 * 3;
504
        avctx->pix_fmt = PIX_FMT_YUV420P;
505
        av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
506
        break;
507
    default:
508
        av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
509
        return 1;
510
    }
511

    
512
    /* Detect compression method */
513
    c->compression = (int8_t)avctx->extradata[5];
514
    switch (avctx->codec_id) {
515
    case CODEC_ID_MSZH:
516
        switch (c->compression) {
517
        case COMP_MSZH:
518
            av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
519
            break;
520
        case COMP_MSZH_NOCOMP:
521
            c->decomp_size = 0;
522
            av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
523
            break;
524
        default:
525
            av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
526
            return 1;
527
        }
528
        break;
529
#if CONFIG_ZLIB_DECODER
530
    case CODEC_ID_ZLIB:
531
        switch (c->compression) {
532
        case COMP_ZLIB_HISPEED:
533
            av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
534
            break;
535
        case COMP_ZLIB_HICOMP:
536
            av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
537
            break;
538
        case COMP_ZLIB_NORMAL:
539
            av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
540
            break;
541
        default:
542
            if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
543
                av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
544
                return 1;
545
            }
546
            av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
547
        }
548
        break;
549
#endif
550
    default:
551
        av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
552
        return 1;
553
    }
554

    
555
    /* Allocate decompression buffer */
556
    if (c->decomp_size) {
557
        if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
558
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
559
            return 1;
560
        }
561
    }
562

    
563
    /* Detect flags */
564
    c->flags = avctx->extradata[6];
565
    if (c->flags & FLAG_MULTITHREAD)
566
        av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
567
    if (c->flags & FLAG_NULLFRAME)
568
        av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
569
    if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
570
        av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
571
    if (c->flags & FLAGMASK_UNUSED)
572
        av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
573

    
574
    /* If needed init zlib */
575
#if CONFIG_ZLIB_DECODER
576
    if (avctx->codec_id == CODEC_ID_ZLIB) {
577
        int zret;
578
        c->zstream.zalloc = Z_NULL;
579
        c->zstream.zfree = Z_NULL;
580
        c->zstream.opaque = Z_NULL;
581
        zret = inflateInit(&c->zstream);
582
        if (zret != Z_OK) {
583
            av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
584
            return 1;
585
        }
586
    }
587
#endif
588

    
589
    return 0;
590
}
591

    
592
/*
593
 *
594
 * Uninit lcl decoder
595
 *
596
 */
597
static av_cold int decode_end(AVCodecContext *avctx)
598
{
599
    LclDecContext * const c = avctx->priv_data;
600

    
601
    if (c->pic.data[0])
602
        avctx->release_buffer(avctx, &c->pic);
603
#if CONFIG_ZLIB_DECODER
604
    inflateEnd(&c->zstream);
605
#endif
606

    
607
    return 0;
608
}
609

    
610
#if CONFIG_MSZH_DECODER
611
AVCodec mszh_decoder = {
612
    "mszh",
613
    CODEC_TYPE_VIDEO,
614
    CODEC_ID_MSZH,
615
    sizeof(LclDecContext),
616
    decode_init,
617
    NULL,
618
    decode_end,
619
    decode_frame,
620
    CODEC_CAP_DR1,
621
    .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
622
};
623
#endif
624

    
625
#if CONFIG_ZLIB_DECODER
626
AVCodec zlib_decoder = {
627
    "zlib",
628
    CODEC_TYPE_VIDEO,
629
    CODEC_ID_ZLIB,
630
    sizeof(LclDecContext),
631
    decode_init,
632
    NULL,
633
    decode_end,
634
    decode_frame,
635
    CODEC_CAP_DR1,
636
    .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
637
};
638
#endif