~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/lcl.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/**
 
24
 * @file lcl.c
 
25
 * LCL (LossLess Codec Library) Video Codec
 
26
 * Decoder for MSZH and ZLIB codecs
 
27
 * Experimental encoder for ZLIB RGB24
 
28
 *
 
29
 * Fourcc: MSZH, ZLIB
 
30
 *
 
31
 * Original Win32 dll:
 
32
 * Ver2.23 By Kenji Oshima 2000.09.20
 
33
 * avimszh.dll, avizlib.dll
 
34
 *
 
35
 * A description of the decoding algorithm can be found here:
 
36
 *   http://www.pcisys.net/~melanson/codecs
 
37
 *
 
38
 * Supports: BGR24 (RGB 24bpp)
 
39
 *
 
40
 */
 
41
 
 
42
#include <stdio.h>
 
43
#include <stdlib.h>
 
44
 
 
45
#include "common.h"
 
46
#include "bitstream.h"
 
47
#include "avcodec.h"
 
48
 
 
49
#ifdef CONFIG_ZLIB
 
50
#include <zlib.h>
 
51
#endif
 
52
 
 
53
 
 
54
#define BMPTYPE_YUV 1
 
55
#define BMPTYPE_RGB 2
 
56
 
 
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
 
63
 
 
64
#define COMP_MSZH 0
 
65
#define COMP_MSZH_NOCOMP 1
 
66
#define COMP_ZLIB_HISPEED 1
 
67
#define COMP_ZLIB_HICOMP 9
 
68
#define COMP_ZLIB_NORMAL -1
 
69
 
 
70
#define FLAG_MULTITHREAD 1
 
71
#define FLAG_NULLFRAME 2
 
72
#define FLAG_PNGFILTER 4
 
73
#define FLAGMASK_UNUSED 0xf8
 
74
 
 
75
#define CODEC_MSZH 1
 
76
#define CODEC_ZLIB 3
 
77
 
 
78
#define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
 
79
#define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
 
80
 
 
81
/*
 
82
 * Decoder context
 
83
 */
 
84
typedef struct LclContext {
 
85
 
 
86
        AVCodecContext *avctx;
 
87
        AVFrame pic;
 
88
    PutBitContext pb;
 
89
 
 
90
    // Image type
 
91
    int imgtype;
 
92
    // Compression type
 
93
    int compression;
 
94
    // Flags
 
95
    int flags;
 
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;
 
104
#ifdef CONFIG_ZLIB
 
105
    z_stream zstream;
 
106
#endif
 
107
} LclContext;
 
108
 
 
109
 
 
110
/*
 
111
 *
 
112
 * Helper functions
 
113
 *
 
114
 */
 
115
static inline unsigned char fix (int pix14)
 
116
{
 
117
    int tmp;
 
118
 
 
119
    tmp = (pix14 + 0x80000) >> 20;
 
120
    if (tmp < 0)
 
121
        return 0;
 
122
    if (tmp > 255)
 
123
        return 255;
 
124
    return tmp;
 
125
}
 
126
 
 
127
 
 
128
 
 
129
static inline unsigned char get_b (unsigned char yq, signed char bq)
 
130
{
 
131
    return fix((yq << 20) + bq * 1858076);
 
132
}
 
133
 
 
134
 
 
135
 
 
136
static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
 
137
{
 
138
    return fix((yq << 20) - bq * 360857 - rq * 748830);
 
139
}
 
140
 
 
141
 
 
142
 
 
143
static inline unsigned char get_r (unsigned char yq, signed char rq)
 
144
{
 
145
    return fix((yq << 20) + rq * 1470103);
 
146
}
 
147
 
 
148
 
 
149
 
 
150
static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
 
151
{
 
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;
 
157
 
 
158
    while ((srclen > 0) && (destptr < destptr_end)) {
 
159
        if (maskbit == 0) {
 
160
            mask = *(srcptr++);
 
161
            maskbit = 8;
 
162
            srclen--;
 
163
            continue;
 
164
        }
 
165
        if ((mask & (1 << (--maskbit))) == 0) {
 
166
            if (destptr + 4 > destptr_end)
 
167
                break;
 
168
            *(int*)destptr = *(int*)srcptr;
 
169
            srclen -= 4;
 
170
            destptr += 4;
 
171
            srcptr += 4;
 
172
        } else {
 
173
            ofs = *(srcptr++);
 
174
            cnt = *(srcptr++);
 
175
            ofs += cnt * 256;;
 
176
            cnt = ((cnt >> 3) & 0x1f) + 1;
 
177
            ofs &= 0x7ff;
 
178
            srclen -= 2;
 
179
            cnt *= 4;
 
180
            if (destptr + cnt > destptr_end) {
 
181
                cnt =  destptr_end - destptr;
 
182
            }
 
183
            for (; cnt > 0; cnt--) {
 
184
                *(destptr) = *(destptr - ofs);
 
185
                destptr++;
 
186
            }
 
187
        }
 
188
    }
 
189
 
 
190
    return (destptr - destptr_bak);
 
191
}
 
192
 
 
193
 
 
194
 
 
195
#ifdef CONFIG_DECODERS
 
196
/*
 
197
 *
 
198
 * Decode a frame
 
199
 *
 
200
 */
 
201
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
 
202
{
 
203
        LclContext * const c = (LclContext *)avctx->priv_data;
 
204
        unsigned char *encoded = (unsigned char *)buf;
 
205
    unsigned int pixel_ptr;
 
206
    int row, col;
 
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;
 
212
    int uqvq;
 
213
    unsigned int mthread_inlen, mthread_outlen;
 
214
#ifdef CONFIG_ZLIB
 
215
    int zret; // Zlib return code
 
216
#endif
 
217
    unsigned int len = buf_size;
 
218
 
 
219
        if(c->pic.data[0])
 
220
                avctx->release_buffer(avctx, &c->pic);
 
221
 
 
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");
 
226
                return -1;
 
227
        }
 
228
 
 
229
    outptr = c->pic.data[0]; // Output image pointer
 
230
 
 
231
    /* Decompress frame */
 
232
    switch (avctx->codec_id) {
 
233
        case CODEC_ID_MSZH:
 
234
            switch (c->compression) {
 
235
                case COMP_MSZH:
 
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);
 
245
                            return -1;
 
246
                        }
 
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);
 
252
                            return -1;
 
253
                        }
 
254
                        encoded = c->decomp_buf;
 
255
                        len = c->decomp_size;
 
256
                    } else {
 
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);
 
261
                            return -1;
 
262
                        }
 
263
                        encoded = c->decomp_buf;
 
264
                        len = mszh_dlen;
 
265
                    }
 
266
                    break;
 
267
                case COMP_MSZH_NOCOMP:
 
268
                    break;
 
269
                default:
 
270
                    av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
 
271
                    return -1;
 
272
            }
 
273
            break;
 
274
        case CODEC_ID_ZLIB:
 
275
#ifdef CONFIG_ZLIB
 
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))
 
281
                break;
 
282
            zret = inflateReset(&(c->zstream));
 
283
            if (zret != Z_OK) {
 
284
                av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
 
285
                return -1;
 
286
            }
 
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);
 
299
                    return -1;
 
300
                }
 
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);
 
304
                    return -1;
 
305
                }
 
306
                zret = inflateReset(&(c->zstream));
 
307
                if (zret != Z_OK) {
 
308
                    av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
 
309
                    return -1;
 
310
                }
 
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);
 
318
                    return -1;
 
319
                }
 
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);
 
323
                    return -1;
 
324
                }
 
325
            } else {
 
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);
 
333
                    return -1;
 
334
                }
 
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);
 
338
                    return -1;
 
339
                }
 
340
            }
 
341
            encoded = c->decomp_buf;
 
342
            len = c->decomp_size;;
 
343
#else
 
344
            av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
 
345
            return -1;
 
346
#endif
 
347
            break;
 
348
        default:
 
349
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
 
350
            return -1;
 
351
    }
 
352
 
 
353
 
 
354
    /* Apply PNG filter */
 
355
    if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
 
356
        switch (c->imgtype) {
 
357
            case IMGTYPE_YUV111:
 
358
            case IMGTYPE_RGB24:
 
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;
 
369
                        pixel_ptr += 3;
 
370
                    }
 
371
                }
 
372
                break;
 
373
            case IMGTYPE_YUV422:
 
374
                for (row = 0; row < height; row++) {
 
375
                    pixel_ptr = row * width * 2;
 
376
                    yq = uq = vq =0;
 
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];
 
386
                        pixel_ptr += 8;
 
387
                    }
 
388
                }
 
389
                break;
 
390
            case IMGTYPE_YUV411:
 
391
                for (row = 0; row < height; row++) {
 
392
                    pixel_ptr = row * width / 2 * 3;
 
393
                    yq = uq = vq =0;
 
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];
 
401
                        pixel_ptr += 6;
 
402
                    }
 
403
                }
 
404
                break;
 
405
            case IMGTYPE_YUV211:
 
406
                for (row = 0; row < height; row++) {
 
407
                    pixel_ptr = row * width * 2;
 
408
                    yq = uq = vq =0;
 
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];
 
414
                        pixel_ptr += 4;
 
415
                    }
 
416
                }
 
417
                break;
 
418
            case IMGTYPE_YUV420:
 
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];
 
429
                        pixel_ptr += 6;
 
430
                    }
 
431
                }
 
432
                break;
 
433
            default:
 
434
                av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
 
435
                return -1;
 
436
        }
 
437
    }
 
438
 
 
439
    /* Convert colorspace */
 
440
    switch (c->imgtype) {
 
441
        case IMGTYPE_YUV111:
 
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]);
 
448
                    encoded += 3;
 
449
                }
 
450
            }
 
451
            break;
 
452
        case IMGTYPE_YUV422:
 
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]);
 
468
                    encoded += 8;
 
469
                }
 
470
            }
 
471
            break;
 
472
        case IMGTYPE_RGB24:
 
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];
 
479
                    encoded += 3;
 
480
                }
 
481
            }
 
482
            break;
 
483
        case IMGTYPE_YUV411:
 
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]);
 
499
                    encoded += 6;
 
500
                }
 
501
            }
 
502
            break;
 
503
        case IMGTYPE_YUV211:
 
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]);
 
513
                    encoded += 4;
 
514
                }
 
515
            }
 
516
            break;
 
517
        case IMGTYPE_YUV420:
 
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]);
 
533
                    pixel_ptr += 6;
 
534
                    encoded += 6;
 
535
                }
 
536
            }
 
537
            break;
 
538
        default:
 
539
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
 
540
            return -1;
 
541
    }
 
542
 
 
543
    *data_size = sizeof(AVFrame);
 
544
    *(AVFrame*)data = c->pic;
 
545
 
 
546
    /* always report that the buffer was completely consumed */
 
547
    return buf_size;
 
548
}
 
549
#endif
 
550
 
 
551
#ifdef CONFIG_ENCODERS
 
552
/*
 
553
 *
 
554
 * Encode a frame
 
555
 *
 
556
 */
 
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;
 
561
    int i;
 
562
    int zret; // Zlib return code
 
563
 
 
564
#ifndef CONFIG_ZLIB
 
565
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
 
566
    return -1;
 
567
#else
 
568
 
 
569
    init_put_bits(&c->pb, buf, buf_size);
 
570
 
 
571
    *p = *pict;
 
572
    p->pict_type= FF_I_TYPE;
 
573
    p->key_frame= 1;
 
574
 
 
575
    if(avctx->pix_fmt != PIX_FMT_BGR24){
 
576
        av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
 
577
        return -1;
 
578
    }
 
579
 
 
580
    zret = deflateReset(&(c->zstream));
 
581
    if (zret != Z_OK) {
 
582
        av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
 
583
        return -1;
 
584
    }
 
585
    c->zstream.next_out = c->comp_buf;
 
586
    c->zstream.avail_out = c->max_comp_size;
 
587
 
 
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);
 
592
        if (zret != Z_OK) {
 
593
            av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
 
594
            return -1;
 
595
        }
 
596
    }
 
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);
 
600
        return -1;
 
601
    }
 
602
 
 
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);
 
606
 
 
607
    return c->zstream.total_out;
 
608
#endif
 
609
}
 
610
#endif /* CONFIG_ENCODERS */
 
611
 
 
612
#ifdef CONFIG_DECODERS
 
613
/*
 
614
 *
 
615
 * Init lcl decoder
 
616
 *
 
617
 */
 
618
static int decode_init(AVCodecContext *avctx)
 
619
{
 
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
 
625
 
 
626
    c->avctx = avctx;
 
627
    avctx->has_b_frames = 0;
 
628
 
 
629
    c->pic.data[0] = NULL;
 
630
 
 
631
#ifdef CONFIG_ZLIB
 
632
    // Needed if zlib unused or init aborted before inflateInit
 
633
    memset(&(c->zstream), 0, sizeof(z_stream));
 
634
#endif
 
635
 
 
636
    if (avctx->extradata_size < 8) {
 
637
        av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
 
638
        return 1;
 
639
    }
 
640
 
 
641
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
 
642
        return 1;
 
643
    }
 
644
 
 
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");
 
649
    }
 
650
 
 
651
    /* Detect image type */
 
652
    switch (c->imgtype = *((char *)avctx->extradata + 4)) {
 
653
        case IMGTYPE_YUV111:
 
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");
 
657
            break;
 
658
        case IMGTYPE_YUV422:
 
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");
 
662
            break;
 
663
        case IMGTYPE_RGB24:
 
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");
 
667
            break;
 
668
        case IMGTYPE_YUV411:
 
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");
 
672
            break;
 
673
        case IMGTYPE_YUV211:
 
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");
 
677
            break;
 
678
        case IMGTYPE_YUV420:
 
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");
 
682
            break;
 
683
        default:
 
684
            av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
 
685
            return 1;
 
686
    }
 
687
 
 
688
    /* Detect compression method */
 
689
    c->compression = *((char *)avctx->extradata + 5);
 
690
    switch (avctx->codec_id) {
 
691
        case CODEC_ID_MSZH:
 
692
            switch (c->compression) {
 
693
                case COMP_MSZH:
 
694
                    av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
 
695
                    break;
 
696
                case COMP_MSZH_NOCOMP:
 
697
                    c->decomp_size = 0;
 
698
                    av_log(avctx, AV_LOG_INFO, "No compression.\n");
 
699
                    break;
 
700
                default:
 
701
                    av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
 
702
                    return 1;
 
703
            }
 
704
            break;
 
705
        case CODEC_ID_ZLIB:
 
706
#ifdef CONFIG_ZLIB
 
707
            switch (c->compression) {
 
708
                case COMP_ZLIB_HISPEED:
 
709
                    av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
 
710
                    break;
 
711
                case COMP_ZLIB_HICOMP:
 
712
                    av_log(avctx, AV_LOG_INFO, "High compression.\n");
 
713
                    break;
 
714
                case COMP_ZLIB_NORMAL:
 
715
                    av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
 
716
                    break;
 
717
                default:
 
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);
 
720
                        return 1;
 
721
                    }
 
722
                    av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
 
723
            }
 
724
#else
 
725
            av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
 
726
            return 1;
 
727
#endif
 
728
            break;
 
729
        default:
 
730
            av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
 
731
            return 1;
 
732
    }
 
733
 
 
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");
 
738
            return 1;
 
739
        }
 
740
    }
 
741
 
 
742
    /* Detect flags */
 
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);
 
752
 
 
753
    /* If needed init zlib */
 
754
    if (avctx->codec_id == CODEC_ID_ZLIB) {
 
755
#ifdef CONFIG_ZLIB
 
756
        c->zstream.zalloc = Z_NULL;
 
757
        c->zstream.zfree = Z_NULL;
 
758
        c->zstream.opaque = Z_NULL;
 
759
        zret = inflateInit(&(c->zstream));
 
760
        if (zret != Z_OK) {
 
761
            av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
 
762
            return 1;
 
763
        }
 
764
#else
 
765
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
 
766
    return 1;
 
767
#endif
 
768
    }
 
769
 
 
770
    avctx->pix_fmt = PIX_FMT_BGR24;
 
771
 
 
772
    return 0;
 
773
}
 
774
#endif /* CONFIG_DECODERS */
 
775
 
 
776
#ifdef CONFIG_ENCODERS
 
777
/*
 
778
 *
 
779
 * Init lcl encoder
 
780
 *
 
781
 */
 
782
static int encode_init(AVCodecContext *avctx)
 
783
{
 
784
    LclContext *c = avctx->priv_data;
 
785
    int zret; // Zlib return code
 
786
 
 
787
#ifndef CONFIG_ZLIB
 
788
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
 
789
    return 1;
 
790
#else
 
791
 
 
792
    c->avctx= avctx;
 
793
 
 
794
    assert(avctx->width && avctx->height);
 
795
 
 
796
    avctx->extradata= av_mallocz(8);
 
797
    avctx->coded_frame= &c->pic;
 
798
 
 
799
    // Will be user settable someday
 
800
    c->compression = 6;
 
801
    c->flags = 0;
 
802
 
 
803
    switch(avctx->pix_fmt){
 
804
        case PIX_FMT_BGR24:
 
805
            c->imgtype = IMGTYPE_RGB24;
 
806
            c->decomp_size = avctx->width * avctx->height * 3;
 
807
            avctx->bits_per_sample= 24;
 
808
            break;
 
809
        default:
 
810
            av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
 
811
            return -1;
 
812
    }
 
813
 
 
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;
 
823
 
 
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);
 
828
    if (zret != Z_OK) {
 
829
        av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
 
830
        return 1;
 
831
    }
 
832
 
 
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");
 
838
        return 1;
 
839
    }
 
840
 
 
841
    return 0;
 
842
#endif
 
843
}
 
844
#endif /* CONFIG_ENCODERS */
 
845
 
 
846
 
 
847
 
 
848
#ifdef CONFIG_DECODERS
 
849
/*
 
850
 *
 
851
 * Uninit lcl decoder
 
852
 *
 
853
 */
 
854
static int decode_end(AVCodecContext *avctx)
 
855
{
 
856
        LclContext * const c = (LclContext *)avctx->priv_data;
 
857
 
 
858
        if (c->pic.data[0])
 
859
                avctx->release_buffer(avctx, &c->pic);
 
860
#ifdef CONFIG_ZLIB
 
861
    inflateEnd(&(c->zstream));
 
862
#endif
 
863
 
 
864
        return 0;
 
865
}
 
866
#endif
 
867
 
 
868
#ifdef CONFIG_ENCODERS
 
869
/*
 
870
 *
 
871
 * Uninit lcl encoder
 
872
 *
 
873
 */
 
874
static int encode_end(AVCodecContext *avctx)
 
875
{
 
876
    LclContext *c = avctx->priv_data;
 
877
 
 
878
    av_freep(&avctx->extradata);
 
879
    av_freep(&c->comp_buf);
 
880
#ifdef CONFIG_ZLIB
 
881
    deflateEnd(&(c->zstream));
 
882
#endif
 
883
 
 
884
    return 0;
 
885
}
 
886
#endif
 
887
 
 
888
#ifdef CONFIG_MSZH_DECODER
 
889
AVCodec mszh_decoder = {
 
890
        "mszh",
 
891
        CODEC_TYPE_VIDEO,
 
892
        CODEC_ID_MSZH,
 
893
        sizeof(LclContext),
 
894
        decode_init,
 
895
        NULL,
 
896
        decode_end,
 
897
        decode_frame,
 
898
        CODEC_CAP_DR1,
 
899
};
 
900
#endif
 
901
 
 
902
#ifdef CONFIG_ZLIB_DECODER
 
903
AVCodec zlib_decoder = {
 
904
        "zlib",
 
905
        CODEC_TYPE_VIDEO,
 
906
        CODEC_ID_ZLIB,
 
907
        sizeof(LclContext),
 
908
        decode_init,
 
909
        NULL,
 
910
        decode_end,
 
911
        decode_frame,
 
912
        CODEC_CAP_DR1,
 
913
};
 
914
#endif
 
915
 
 
916
#ifdef CONFIG_ENCODERS
 
917
 
 
918
AVCodec zlib_encoder = {
 
919
    "zlib",
 
920
    CODEC_TYPE_VIDEO,
 
921
    CODEC_ID_ZLIB,
 
922
    sizeof(LclContext),
 
923
    encode_init,
 
924
    encode_frame,
 
925
    encode_end,
 
926
};
 
927
 
 
928
#endif //CONFIG_ENCODERS