ffmpeg / libavcodec / lcldec.c @ 9106a698
History | View | Annotate | Download (25 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
|
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
|
69 |
z_stream zstream; |
70 |
#endif
|
71 |
} LclDecContext; |
72 |
|
73 |
|
74 |
/*
|
75 |
*
|
76 |
* Helper functions
|
77 |
*
|
78 |
*/
|
79 |
static inline unsigned char fix (int pix14) |
80 |
{ |
81 |
int tmp;
|
82 |
|
83 |
tmp = (pix14 + 0x80000) >> 20; |
84 |
if (tmp < 0) |
85 |
return 0; |
86 |
if (tmp > 255) |
87 |
return 255; |
88 |
return tmp;
|
89 |
} |
90 |
|
91 |
|
92 |
|
93 |
static inline unsigned char get_b (unsigned char yq, signed char bq) |
94 |
{ |
95 |
return fix((yq << 20) + bq * 1858076); |
96 |
} |
97 |
|
98 |
|
99 |
|
100 |
static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq) |
101 |
{ |
102 |
return fix((yq << 20) - bq * 360857 - rq * 748830); |
103 |
} |
104 |
|
105 |
|
106 |
|
107 |
static inline unsigned char get_r (unsigned char yq, signed char rq) |
108 |
{ |
109 |
return fix((yq << 20) + rq * 1470103); |
110 |
} |
111 |
|
112 |
|
113 |
|
114 |
static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize) |
115 |
{ |
116 |
unsigned char *destptr_bak = destptr; |
117 |
unsigned char *destptr_end = destptr + destsize; |
118 |
unsigned char mask = 0; |
119 |
unsigned char maskbit = 0; |
120 |
unsigned int ofs, cnt; |
121 |
|
122 |
while ((srclen > 0) && (destptr < destptr_end)) { |
123 |
if (maskbit == 0) { |
124 |
mask = *(srcptr++); |
125 |
maskbit = 8;
|
126 |
srclen--; |
127 |
continue;
|
128 |
} |
129 |
if ((mask & (1 << (--maskbit))) == 0) { |
130 |
if (destptr + 4 > destptr_end) |
131 |
break;
|
132 |
*(int*)destptr = *(int*)srcptr; |
133 |
srclen -= 4;
|
134 |
destptr += 4;
|
135 |
srcptr += 4;
|
136 |
} else {
|
137 |
ofs = *(srcptr++); |
138 |
cnt = *(srcptr++); |
139 |
ofs += cnt * 256;
|
140 |
cnt = ((cnt >> 3) & 0x1f) + 1; |
141 |
ofs &= 0x7ff;
|
142 |
srclen -= 2;
|
143 |
cnt *= 4;
|
144 |
if (destptr + cnt > destptr_end) {
|
145 |
cnt = destptr_end - destptr; |
146 |
} |
147 |
for (; cnt > 0; cnt--) { |
148 |
*(destptr) = *(destptr - ofs); |
149 |
destptr++; |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
return (destptr - destptr_bak);
|
155 |
} |
156 |
|
157 |
|
158 |
|
159 |
/*
|
160 |
*
|
161 |
* Decode a frame
|
162 |
*
|
163 |
*/
|
164 |
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) |
165 |
{ |
166 |
const uint8_t *buf = avpkt->data;
|
167 |
int buf_size = avpkt->size;
|
168 |
LclDecContext * const c = avctx->priv_data;
|
169 |
unsigned char *encoded = (unsigned char *)buf; |
170 |
unsigned int pixel_ptr; |
171 |
int row, col;
|
172 |
unsigned char *outptr; |
173 |
unsigned int width = avctx->width; // Real image width |
174 |
unsigned int height = avctx->height; // Real image height |
175 |
unsigned int mszh_dlen; |
176 |
unsigned char yq, y1q, uq, vq; |
177 |
int uqvq;
|
178 |
unsigned int mthread_inlen, mthread_outlen; |
179 |
#if CONFIG_ZLIB
|
180 |
int zret; // Zlib return code |
181 |
#endif
|
182 |
unsigned int len = buf_size; |
183 |
|
184 |
if(c->pic.data[0]) |
185 |
avctx->release_buffer(avctx, &c->pic); |
186 |
|
187 |
c->pic.reference = 0;
|
188 |
c->pic.buffer_hints = FF_BUFFER_HINTS_VALID; |
189 |
if(avctx->get_buffer(avctx, &c->pic) < 0){ |
190 |
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
191 |
return -1; |
192 |
} |
193 |
|
194 |
outptr = c->pic.data[0]; // Output image pointer |
195 |
|
196 |
/* Decompress frame */
|
197 |
switch (avctx->codec_id) {
|
198 |
case CODEC_ID_MSZH:
|
199 |
switch (c->compression) {
|
200 |
case COMP_MSZH:
|
201 |
if (c->flags & FLAG_MULTITHREAD) {
|
202 |
mthread_inlen = *((unsigned int*)encoded); |
203 |
mthread_outlen = *((unsigned int*)(encoded+4)); |
204 |
if (mthread_outlen > c->decomp_size) // this should not happen |
205 |
mthread_outlen = c->decomp_size; |
206 |
mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
|
207 |
if (mthread_outlen != mszh_dlen) {
|
208 |
av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
|
209 |
mthread_outlen, mszh_dlen); |
210 |
return -1; |
211 |
} |
212 |
mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
|
213 |
c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen); |
214 |
if (mthread_outlen != mszh_dlen) {
|
215 |
av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
|
216 |
mthread_outlen, mszh_dlen); |
217 |
return -1; |
218 |
} |
219 |
encoded = c->decomp_buf; |
220 |
len = c->decomp_size; |
221 |
} else {
|
222 |
mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size); |
223 |
if (c->decomp_size != mszh_dlen) {
|
224 |
av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
|
225 |
c->decomp_size, mszh_dlen); |
226 |
return -1; |
227 |
} |
228 |
encoded = c->decomp_buf; |
229 |
len = mszh_dlen; |
230 |
} |
231 |
break;
|
232 |
case COMP_MSZH_NOCOMP:
|
233 |
break;
|
234 |
default:
|
235 |
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
|
236 |
return -1; |
237 |
} |
238 |
break;
|
239 |
case CODEC_ID_ZLIB:
|
240 |
#if CONFIG_ZLIB
|
241 |
/* Using the original dll with normal compression (-1) and RGB format
|
242 |
* gives a file with ZLIB fourcc, but frame is really uncompressed.
|
243 |
* To be sure that's true check also frame size */
|
244 |
if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
|
245 |
(len == width * height * 3))
|
246 |
break;
|
247 |
zret = inflateReset(&(c->zstream)); |
248 |
if (zret != Z_OK) {
|
249 |
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
|
250 |
return -1; |
251 |
} |
252 |
if (c->flags & FLAG_MULTITHREAD) {
|
253 |
mthread_inlen = *((unsigned int*)encoded); |
254 |
mthread_outlen = *((unsigned int*)(encoded+4)); |
255 |
if (mthread_outlen > c->decomp_size)
|
256 |
mthread_outlen = c->decomp_size; |
257 |
c->zstream.next_in = encoded + 8;
|
258 |
c->zstream.avail_in = mthread_inlen; |
259 |
c->zstream.next_out = c->decomp_buf; |
260 |
c->zstream.avail_out = c->decomp_size; |
261 |
zret = inflate(&(c->zstream), Z_FINISH); |
262 |
if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
|
263 |
av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
|
264 |
return -1; |
265 |
} |
266 |
if (mthread_outlen != (unsigned int)(c->zstream.total_out)) { |
267 |
av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
|
268 |
mthread_outlen, c->zstream.total_out); |
269 |
return -1; |
270 |
} |
271 |
zret = inflateReset(&(c->zstream)); |
272 |
if (zret != Z_OK) {
|
273 |
av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
|
274 |
return -1; |
275 |
} |
276 |
c->zstream.next_in = encoded + 8 + mthread_inlen;
|
277 |
c->zstream.avail_in = len - mthread_inlen; |
278 |
c->zstream.next_out = c->decomp_buf + mthread_outlen; |
279 |
c->zstream.avail_out = c->decomp_size - mthread_outlen; |
280 |
zret = inflate(&(c->zstream), Z_FINISH); |
281 |
if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
|
282 |
av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
|
283 |
return -1; |
284 |
} |
285 |
if (mthread_outlen != (unsigned int)(c->zstream.total_out)) { |
286 |
av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
|
287 |
mthread_outlen, c->zstream.total_out); |
288 |
return -1; |
289 |
} |
290 |
} else {
|
291 |
c->zstream.next_in = encoded; |
292 |
c->zstream.avail_in = len; |
293 |
c->zstream.next_out = c->decomp_buf; |
294 |
c->zstream.avail_out = c->decomp_size; |
295 |
zret = inflate(&(c->zstream), Z_FINISH); |
296 |
if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
|
297 |
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
|
298 |
return -1; |
299 |
} |
300 |
if (c->decomp_size != (unsigned int)(c->zstream.total_out)) { |
301 |
av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
|
302 |
c->decomp_size, c->zstream.total_out); |
303 |
return -1; |
304 |
} |
305 |
} |
306 |
encoded = c->decomp_buf; |
307 |
len = c->decomp_size; |
308 |
#else
|
309 |
av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
|
310 |
return -1; |
311 |
#endif
|
312 |
break;
|
313 |
default:
|
314 |
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
|
315 |
return -1; |
316 |
} |
317 |
|
318 |
|
319 |
/* Apply PNG filter */
|
320 |
if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
|
321 |
switch (c->imgtype) {
|
322 |
case IMGTYPE_YUV111:
|
323 |
case IMGTYPE_RGB24:
|
324 |
for (row = 0; row < height; row++) { |
325 |
pixel_ptr = row * width * 3;
|
326 |
yq = encoded[pixel_ptr++]; |
327 |
uqvq = AV_RL16(encoded+pixel_ptr); |
328 |
pixel_ptr += 2;
|
329 |
for (col = 1; col < width; col++) { |
330 |
encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
331 |
uqvq -= AV_RL16(encoded+pixel_ptr+1);
|
332 |
AV_WL16(encoded+pixel_ptr+1, uqvq);
|
333 |
pixel_ptr += 3;
|
334 |
} |
335 |
} |
336 |
break;
|
337 |
case IMGTYPE_YUV422:
|
338 |
for (row = 0; row < height; row++) { |
339 |
pixel_ptr = row * width * 2;
|
340 |
yq = uq = vq =0;
|
341 |
for (col = 0; col < width/4; col++) { |
342 |
encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
343 |
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
344 |
encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2]; |
345 |
encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3]; |
346 |
encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; |
347 |
encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5]; |
348 |
encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6]; |
349 |
encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7]; |
350 |
pixel_ptr += 8;
|
351 |
} |
352 |
} |
353 |
break;
|
354 |
case IMGTYPE_YUV411:
|
355 |
for (row = 0; row < height; row++) { |
356 |
pixel_ptr = row * width / 2 * 3; |
357 |
yq = uq = vq =0;
|
358 |
for (col = 0; col < width/4; col++) { |
359 |
encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
360 |
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
361 |
encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2]; |
362 |
encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3]; |
363 |
encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; |
364 |
encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5]; |
365 |
pixel_ptr += 6;
|
366 |
} |
367 |
} |
368 |
break;
|
369 |
case IMGTYPE_YUV211:
|
370 |
for (row = 0; row < height; row++) { |
371 |
pixel_ptr = row * width * 2;
|
372 |
yq = uq = vq =0;
|
373 |
for (col = 0; col < width/2; col++) { |
374 |
encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
375 |
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
376 |
encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2]; |
377 |
encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3]; |
378 |
pixel_ptr += 4;
|
379 |
} |
380 |
} |
381 |
break;
|
382 |
case IMGTYPE_YUV420:
|
383 |
for (row = 0; row < height/2; row++) { |
384 |
pixel_ptr = row * width * 3;
|
385 |
yq = y1q = uq = vq =0;
|
386 |
for (col = 0; col < width/2; col++) { |
387 |
encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
388 |
encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
389 |
encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2]; |
390 |
encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3]; |
391 |
encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; |
392 |
encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5]; |
393 |
pixel_ptr += 6;
|
394 |
} |
395 |
} |
396 |
break;
|
397 |
default:
|
398 |
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
|
399 |
return -1; |
400 |
} |
401 |
} |
402 |
|
403 |
/* Convert colorspace */
|
404 |
switch (c->imgtype) {
|
405 |
case IMGTYPE_YUV111:
|
406 |
for (row = height - 1; row >= 0; row--) { |
407 |
pixel_ptr = row * c->pic.linesize[0];
|
408 |
for (col = 0; col < width; col++) { |
409 |
outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]); |
410 |
outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]); |
411 |
outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]); |
412 |
encoded += 3;
|
413 |
} |
414 |
} |
415 |
break;
|
416 |
case IMGTYPE_YUV422:
|
417 |
for (row = height - 1; row >= 0; row--) { |
418 |
pixel_ptr = row * c->pic.linesize[0];
|
419 |
for (col = 0; col < width/4; col++) { |
420 |
outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]); |
421 |
outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]); |
422 |
outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]); |
423 |
outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]); |
424 |
outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]); |
425 |
outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]); |
426 |
outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]); |
427 |
outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]); |
428 |
outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]); |
429 |
outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]); |
430 |
outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]); |
431 |
outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]); |
432 |
encoded += 8;
|
433 |
} |
434 |
} |
435 |
break;
|
436 |
case IMGTYPE_RGB24:
|
437 |
for (row = height - 1; row >= 0; row--) { |
438 |
pixel_ptr = row * c->pic.linesize[0];
|
439 |
for (col = 0; col < width; col++) { |
440 |
outptr[pixel_ptr++] = encoded[0];
|
441 |
outptr[pixel_ptr++] = encoded[1];
|
442 |
outptr[pixel_ptr++] = encoded[2];
|
443 |
encoded += 3;
|
444 |
} |
445 |
} |
446 |
break;
|
447 |
case IMGTYPE_YUV411:
|
448 |
for (row = height - 1; row >= 0; row--) { |
449 |
pixel_ptr = row * c->pic.linesize[0];
|
450 |
for (col = 0; col < width/4; col++) { |
451 |
outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]); |
452 |
outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]); |
453 |
outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]); |
454 |
outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]); |
455 |
outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]); |
456 |
outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]); |
457 |
outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]); |
458 |
outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]); |
459 |
outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]); |
460 |
outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]); |
461 |
outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]); |
462 |
outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]); |
463 |
encoded += 6;
|
464 |
} |
465 |
} |
466 |
break;
|
467 |
case IMGTYPE_YUV211:
|
468 |
for (row = height - 1; row >= 0; row--) { |
469 |
pixel_ptr = row * c->pic.linesize[0];
|
470 |
for (col = 0; col < width/2; col++) { |
471 |
outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]); |
472 |
outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]); |
473 |
outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]); |
474 |
outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]); |
475 |
outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]); |
476 |
outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]); |
477 |
encoded += 4;
|
478 |
} |
479 |
} |
480 |
break;
|
481 |
case IMGTYPE_YUV420:
|
482 |
for (row = height / 2 - 1; row >= 0; row--) { |
483 |
pixel_ptr = 2 * row * c->pic.linesize[0]; |
484 |
for (col = 0; col < width/2; col++) { |
485 |
outptr[pixel_ptr] = get_b(encoded[0], encoded[4]); |
486 |
outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]); |
487 |
outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]); |
488 |
outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]); |
489 |
outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]); |
490 |
outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]); |
491 |
outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]); |
492 |
outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]); |
493 |
outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]); |
494 |
outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]); |
495 |
outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]); |
496 |
outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]); |
497 |
pixel_ptr += 6;
|
498 |
encoded += 6;
|
499 |
} |
500 |
} |
501 |
break;
|
502 |
default:
|
503 |
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
|
504 |
return -1; |
505 |
} |
506 |
|
507 |
*data_size = sizeof(AVFrame);
|
508 |
*(AVFrame*)data = c->pic; |
509 |
|
510 |
/* always report that the buffer was completely consumed */
|
511 |
return buf_size;
|
512 |
} |
513 |
|
514 |
/*
|
515 |
*
|
516 |
* Init lcl decoder
|
517 |
*
|
518 |
*/
|
519 |
static av_cold int decode_init(AVCodecContext *avctx) |
520 |
{ |
521 |
LclDecContext * const c = avctx->priv_data;
|
522 |
unsigned int basesize = avctx->width * avctx->height; |
523 |
unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3); |
524 |
unsigned int max_decomp_size; |
525 |
int zret; // Zlib return code |
526 |
|
527 |
c->pic.data[0] = NULL; |
528 |
|
529 |
#if CONFIG_ZLIB
|
530 |
// Needed if zlib unused or init aborted before inflateInit
|
531 |
memset(&(c->zstream), 0, sizeof(z_stream)); |
532 |
#endif
|
533 |
|
534 |
if (avctx->extradata_size < 8) { |
535 |
av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
|
536 |
return 1; |
537 |
} |
538 |
|
539 |
if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { |
540 |
return 1; |
541 |
} |
542 |
|
543 |
/* Check codec type */
|
544 |
if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) || |
545 |
((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) { |
546 |
av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
|
547 |
} |
548 |
|
549 |
/* Detect image type */
|
550 |
switch (c->imgtype = *((char *)avctx->extradata + 4)) { |
551 |
case IMGTYPE_YUV111:
|
552 |
c->decomp_size = basesize * 3;
|
553 |
max_decomp_size = max_basesize * 3;
|
554 |
av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
|
555 |
break;
|
556 |
case IMGTYPE_YUV422:
|
557 |
c->decomp_size = basesize * 2;
|
558 |
max_decomp_size = max_basesize * 2;
|
559 |
av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
|
560 |
break;
|
561 |
case IMGTYPE_RGB24:
|
562 |
c->decomp_size = basesize * 3;
|
563 |
max_decomp_size = max_basesize * 3;
|
564 |
av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
|
565 |
break;
|
566 |
case IMGTYPE_YUV411:
|
567 |
c->decomp_size = basesize / 2 * 3; |
568 |
max_decomp_size = max_basesize / 2 * 3; |
569 |
av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
|
570 |
break;
|
571 |
case IMGTYPE_YUV211:
|
572 |
c->decomp_size = basesize * 2;
|
573 |
max_decomp_size = max_basesize * 2;
|
574 |
av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
|
575 |
break;
|
576 |
case IMGTYPE_YUV420:
|
577 |
c->decomp_size = basesize / 2 * 3; |
578 |
max_decomp_size = max_basesize / 2 * 3; |
579 |
av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
|
580 |
break;
|
581 |
default:
|
582 |
av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
|
583 |
return 1; |
584 |
} |
585 |
|
586 |
/* Detect compression method */
|
587 |
c->compression = *((char *)avctx->extradata + 5); |
588 |
switch (avctx->codec_id) {
|
589 |
case CODEC_ID_MSZH:
|
590 |
switch (c->compression) {
|
591 |
case COMP_MSZH:
|
592 |
av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
|
593 |
break;
|
594 |
case COMP_MSZH_NOCOMP:
|
595 |
c->decomp_size = 0;
|
596 |
av_log(avctx, AV_LOG_INFO, "No compression.\n");
|
597 |
break;
|
598 |
default:
|
599 |
av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
|
600 |
return 1; |
601 |
} |
602 |
break;
|
603 |
case CODEC_ID_ZLIB:
|
604 |
#if CONFIG_ZLIB
|
605 |
switch (c->compression) {
|
606 |
case COMP_ZLIB_HISPEED:
|
607 |
av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
|
608 |
break;
|
609 |
case COMP_ZLIB_HICOMP:
|
610 |
av_log(avctx, AV_LOG_INFO, "High compression.\n");
|
611 |
break;
|
612 |
case COMP_ZLIB_NORMAL:
|
613 |
av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
|
614 |
break;
|
615 |
default:
|
616 |
if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
|
617 |
av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
|
618 |
return 1; |
619 |
} |
620 |
av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
|
621 |
} |
622 |
#else
|
623 |
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
|
624 |
return 1; |
625 |
#endif
|
626 |
break;
|
627 |
default:
|
628 |
av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
|
629 |
return 1; |
630 |
} |
631 |
|
632 |
/* Allocate decompression buffer */
|
633 |
if (c->decomp_size) {
|
634 |
if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) { |
635 |
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
|
636 |
return 1; |
637 |
} |
638 |
} |
639 |
|
640 |
/* Detect flags */
|
641 |
c->flags = *((char *)avctx->extradata + 6); |
642 |
if (c->flags & FLAG_MULTITHREAD)
|
643 |
av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
|
644 |
if (c->flags & FLAG_NULLFRAME)
|
645 |
av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
|
646 |
if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
|
647 |
av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
|
648 |
if (c->flags & FLAGMASK_UNUSED)
|
649 |
av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
|
650 |
|
651 |
/* If needed init zlib */
|
652 |
if (avctx->codec_id == CODEC_ID_ZLIB) {
|
653 |
#if CONFIG_ZLIB
|
654 |
c->zstream.zalloc = Z_NULL; |
655 |
c->zstream.zfree = Z_NULL; |
656 |
c->zstream.opaque = Z_NULL; |
657 |
zret = inflateInit(&(c->zstream)); |
658 |
if (zret != Z_OK) {
|
659 |
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
660 |
return 1; |
661 |
} |
662 |
#else
|
663 |
av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
|
664 |
return 1; |
665 |
#endif
|
666 |
} |
667 |
|
668 |
avctx->pix_fmt = PIX_FMT_BGR24; |
669 |
|
670 |
return 0; |
671 |
} |
672 |
|
673 |
/*
|
674 |
*
|
675 |
* Uninit lcl decoder
|
676 |
*
|
677 |
*/
|
678 |
static av_cold int decode_end(AVCodecContext *avctx) |
679 |
{ |
680 |
LclDecContext * const c = avctx->priv_data;
|
681 |
|
682 |
if (c->pic.data[0]) |
683 |
avctx->release_buffer(avctx, &c->pic); |
684 |
#if CONFIG_ZLIB
|
685 |
inflateEnd(&(c->zstream)); |
686 |
#endif
|
687 |
|
688 |
return 0; |
689 |
} |
690 |
|
691 |
#if CONFIG_MSZH_DECODER
|
692 |
AVCodec mszh_decoder = { |
693 |
"mszh",
|
694 |
CODEC_TYPE_VIDEO, |
695 |
CODEC_ID_MSZH, |
696 |
sizeof(LclDecContext),
|
697 |
decode_init, |
698 |
NULL,
|
699 |
decode_end, |
700 |
decode_frame, |
701 |
CODEC_CAP_DR1, |
702 |
.long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
|
703 |
}; |
704 |
#endif
|
705 |
|
706 |
#if CONFIG_ZLIB_DECODER
|
707 |
AVCodec zlib_decoder = { |
708 |
"zlib",
|
709 |
CODEC_TYPE_VIDEO, |
710 |
CODEC_ID_ZLIB, |
711 |
sizeof(LclDecContext),
|
712 |
decode_init, |
713 |
NULL,
|
714 |
decode_end, |
715 |
decode_frame, |
716 |
CODEC_CAP_DR1, |
717 |
.long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
|
718 |
}; |
719 |
#endif
|