Statistics
| Branch: | Revision:

ffmpeg / libavcodec / lcldec.c @ 1207c84a

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

    
150

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

    
174
    if(c->pic.data[0])
175
        avctx->release_buffer(avctx, &c->pic);
176

    
177
    c->pic.reference = 0;
178
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
179
    if(avctx->get_buffer(avctx, &c->pic) < 0){
180
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
181
        return -1;
182
    }
183

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

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

    
259

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

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

    
431
    *data_size = sizeof(AVFrame);
432
    *(AVFrame*)data = c->pic;
433

    
434
    /* always report that the buffer was completely consumed */
435
    return buf_size;
436
}
437

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

    
450
    c->pic.data[0] = NULL;
451

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

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

    
462
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
463
        return 1;
464
    }
465

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

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

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

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

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

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

    
592
    return 0;
593
}
594

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

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

    
610
    return 0;
611
}
612

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

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