Mercurial > libavcodec.hg
annotate lcl.c @ 2398:582e635cfa08 libavcodec
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
common.h -> common.h/bitstream.h
author | michael |
---|---|
date | Wed, 29 Dec 2004 17:50:25 +0000 |
parents | 902caf560c43 |
children | 82af834636c2 |
rev | line source |
---|---|
1743 | 1 /* |
2 * LCL (LossLess Codec Library) Codec | |
3 * Copyright (c) 2002-2004 Roberto Togni | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 * | |
19 */ | |
20 | |
21 /** | |
22 * @file lcl.c | |
23 * LCL (LossLess Codec Library) Video Codec | |
24 * Decoder for MSZH and ZLIB codecs | |
25 * Experimental encoder for ZLIB RGB24 | |
26 * | |
27 * Fourcc: MSZH, ZLIB | |
28 * | |
29 * Original Win32 dll: | |
30 * Ver2.23 By Kenji Oshima 2000.09.20 | |
31 * avimszh.dll, avizlib.dll | |
32 * | |
33 * A description of the decoding algorithm can be found here: | |
34 * http://www.pcisys.net/~melanson/codecs | |
35 * | |
36 * Supports: BGR24 (RGB 24bpp) | |
37 * | |
38 */ | |
39 | |
40 #include <stdio.h> | |
41 #include <stdlib.h> | |
42 | |
43 #include "common.h" | |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2250
diff
changeset
|
44 #include "bitstream.h" |
1743 | 45 #include "avcodec.h" |
46 | |
47 #ifdef CONFIG_ZLIB | |
48 #include <zlib.h> | |
49 #endif | |
50 | |
51 | |
52 #define BMPTYPE_YUV 1 | |
53 #define BMPTYPE_RGB 2 | |
54 | |
55 #define IMGTYPE_YUV111 0 | |
56 #define IMGTYPE_YUV422 1 | |
57 #define IMGTYPE_RGB24 2 | |
58 #define IMGTYPE_YUV411 3 | |
59 #define IMGTYPE_YUV211 4 | |
60 #define IMGTYPE_YUV420 5 | |
61 | |
62 #define COMP_MSZH 0 | |
63 #define COMP_MSZH_NOCOMP 1 | |
64 #define COMP_ZLIB_HISPEED 1 | |
65 #define COMP_ZLIB_HICOMP 9 | |
66 #define COMP_ZLIB_NORMAL -1 | |
67 | |
68 #define FLAG_MULTITHREAD 1 | |
69 #define FLAG_NULLFRAME 2 | |
70 #define FLAG_PNGFILTER 4 | |
71 #define FLAGMASK_UNUSED 0xf8 | |
72 | |
73 #define CODEC_MSZH 1 | |
74 #define CODEC_ZLIB 3 | |
75 | |
76 #define FOURCC_MSZH mmioFOURCC('M','S','Z','H') | |
77 #define FOURCC_ZLIB mmioFOURCC('Z','L','I','B') | |
78 | |
79 /* | |
80 * Decoder context | |
81 */ | |
82 typedef struct LclContext { | |
83 | |
84 AVCodecContext *avctx; | |
85 AVFrame pic; | |
86 PutBitContext pb; | |
87 | |
88 // Image type | |
89 int imgtype; | |
90 // Compression type | |
91 int compression; | |
92 // Flags | |
93 int flags; | |
94 // Decompressed data size | |
95 unsigned int decomp_size; | |
96 // Decompression buffer | |
97 unsigned char* decomp_buf; | |
98 // Maximum compressed data size | |
99 unsigned int max_comp_size; | |
100 // Compression buffer | |
101 unsigned char* comp_buf; | |
102 #ifdef CONFIG_ZLIB | |
103 z_stream zstream; | |
104 #endif | |
105 } LclContext; | |
106 | |
107 | |
108 /* | |
109 * | |
110 * Helper functions | |
111 * | |
112 */ | |
113 static inline unsigned char fix (int pix14) | |
114 { | |
115 int tmp; | |
116 | |
117 tmp = (pix14 + 0x80000) >> 20; | |
118 if (tmp < 0) | |
119 return 0; | |
120 if (tmp > 255) | |
121 return 255; | |
122 return tmp; | |
123 } | |
124 | |
125 | |
126 | |
127 static inline unsigned char get_b (unsigned char yq, signed char bq) | |
128 { | |
129 return fix((yq << 20) + bq * 1858076); | |
130 } | |
131 | |
132 | |
133 | |
134 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq) | |
135 { | |
136 return fix((yq << 20) - bq * 360857 - rq * 748830); | |
137 } | |
138 | |
139 | |
140 | |
141 static inline unsigned char get_r (unsigned char yq, signed char rq) | |
142 { | |
143 return fix((yq << 20) + rq * 1470103); | |
144 } | |
145 | |
146 | |
147 | |
148 static int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr) | |
149 { | |
150 unsigned char *destptr_bak = destptr; | |
151 unsigned char mask = 0; | |
152 unsigned char maskbit = 0; | |
153 unsigned int ofs, cnt; | |
154 | |
155 while (srclen > 0) { | |
156 if (maskbit == 0) { | |
157 mask = *(srcptr++); | |
158 maskbit = 8; | |
159 srclen--; | |
160 continue; | |
161 } | |
162 if ((mask & (1 << (--maskbit))) == 0) { | |
163 *(int*)destptr = *(int*)srcptr; | |
164 srclen -= 4; | |
165 destptr += 4; | |
166 srcptr += 4; | |
167 } else { | |
168 ofs = *(srcptr++); | |
169 cnt = *(srcptr++); | |
170 ofs += cnt * 256;; | |
171 cnt = ((cnt >> 3) & 0x1f) + 1; | |
172 ofs &= 0x7ff; | |
173 srclen -= 2; | |
174 cnt *= 4; | |
175 for (; cnt > 0; cnt--) { | |
176 *(destptr) = *(destptr - ofs); | |
177 destptr++; | |
178 } | |
179 } | |
180 } | |
181 | |
182 return (destptr - destptr_bak); | |
183 } | |
184 | |
185 | |
186 | |
187 | |
188 /* | |
189 * | |
190 * Decode a frame | |
191 * | |
192 */ | |
193 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) | |
194 { | |
195 LclContext * const c = (LclContext *)avctx->priv_data; | |
196 unsigned char *encoded = (unsigned char *)buf; | |
197 int pixel_ptr; | |
198 int row, col; | |
199 unsigned char *outptr; | |
200 unsigned int width = avctx->width; // Real image width | |
201 unsigned int height = avctx->height; // Real image height | |
202 unsigned int mszh_dlen; | |
203 unsigned char yq, y1q, uq, vq; | |
204 int uqvq; | |
205 unsigned int mthread_inlen, mthread_outlen; | |
206 #ifdef CONFIG_ZLIB | |
207 int zret; // Zlib return code | |
208 #endif | |
209 int len = buf_size; | |
210 | |
211 /* no supplementary picture */ | |
212 if (buf_size == 0) | |
213 return 0; | |
214 | |
215 if(c->pic.data[0]) | |
216 avctx->release_buffer(avctx, &c->pic); | |
217 | |
218 c->pic.reference = 0; | |
219 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID; | |
220 if(avctx->get_buffer(avctx, &c->pic) < 0){ | |
221 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
222 return -1; | |
223 } | |
224 | |
225 outptr = c->pic.data[0]; // Output image pointer | |
226 | |
227 /* Decompress frame */ | |
228 switch (avctx->codec_id) { | |
229 case CODEC_ID_MSZH: | |
230 switch (c->compression) { | |
231 case COMP_MSZH: | |
232 if (c->flags & FLAG_MULTITHREAD) { | |
233 mthread_inlen = *((unsigned int*)encoded); | |
234 mthread_outlen = *((unsigned int*)(encoded+4)); | |
235 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf); | |
236 if (mthread_outlen != mszh_dlen) { | |
237 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n", | |
238 mthread_outlen, mszh_dlen); | |
239 } | |
240 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen, | |
241 c->decomp_buf + mthread_outlen); | |
242 if ((c->decomp_size - mthread_outlen) != mszh_dlen) { | |
243 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n", | |
244 c->decomp_size - mthread_outlen, mszh_dlen); | |
245 } | |
246 encoded = c->decomp_buf; | |
247 len = c->decomp_size; | |
248 } else { | |
249 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf); | |
250 if (c->decomp_size != mszh_dlen) { | |
251 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n", | |
252 c->decomp_size, mszh_dlen); | |
253 } | |
254 encoded = c->decomp_buf; | |
255 len = mszh_dlen; | |
256 } | |
257 break; | |
258 case COMP_MSZH_NOCOMP: | |
259 break; | |
260 default: | |
261 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n"); | |
262 return -1; | |
263 } | |
264 break; | |
265 case CODEC_ID_ZLIB: | |
266 #ifdef CONFIG_ZLIB | |
267 /* Using the original dll with normal compression (-1) and RGB format | |
268 * gives a file with ZLIB fourcc, but frame is really uncompressed. | |
269 * To be sure that's true check also frame size */ | |
270 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) && | |
271 (len == width * height * 3)) | |
272 break; | |
273 zret = inflateReset(&(c->zstream)); | |
274 if (zret != Z_OK) { | |
275 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret); | |
276 return -1; | |
277 } | |
278 if (c->flags & FLAG_MULTITHREAD) { | |
279 mthread_inlen = *((unsigned int*)encoded); | |
280 mthread_outlen = *((unsigned int*)(encoded+4)); | |
281 c->zstream.next_in = encoded + 8; | |
282 c->zstream.avail_in = mthread_inlen; | |
283 c->zstream.next_out = c->decomp_buf; | |
284 c->zstream.avail_out = mthread_outlen; | |
285 zret = inflate(&(c->zstream), Z_FINISH); | |
286 if ((zret != Z_OK) && (zret != Z_STREAM_END)) { | |
287 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret); | |
288 return -1; | |
289 } | |
290 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) { | |
291 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n", | |
292 mthread_outlen, c->zstream.total_out); | |
293 } | |
294 zret = inflateReset(&(c->zstream)); | |
295 if (zret != Z_OK) { | |
296 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret); | |
297 return -1; | |
298 } | |
299 c->zstream.next_in = encoded + 8 + mthread_inlen; | |
300 c->zstream.avail_in = len - mthread_inlen; | |
301 c->zstream.next_out = c->decomp_buf + mthread_outlen; | |
302 c->zstream.avail_out = mthread_outlen; | |
303 zret = inflate(&(c->zstream), Z_FINISH); | |
304 if ((zret != Z_OK) && (zret != Z_STREAM_END)) { | |
305 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret); | |
306 return -1; | |
307 } | |
308 if ((c->decomp_size - mthread_outlen) != (unsigned int)(c->zstream.total_out)) { | |
309 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n", | |
310 c->decomp_size - mthread_outlen, c->zstream.total_out); | |
311 } | |
312 } else { | |
313 c->zstream.next_in = encoded; | |
314 c->zstream.avail_in = len; | |
315 c->zstream.next_out = c->decomp_buf; | |
316 c->zstream.avail_out = c->decomp_size; | |
317 zret = inflate(&(c->zstream), Z_FINISH); | |
318 if ((zret != Z_OK) && (zret != Z_STREAM_END)) { | |
319 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret); | |
320 return -1; | |
321 } | |
322 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) { | |
323 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n", | |
324 c->decomp_size, c->zstream.total_out); | |
325 } | |
326 } | |
327 encoded = c->decomp_buf; | |
328 len = c->decomp_size;; | |
329 #else | |
330 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n"); | |
331 return -1; | |
332 #endif | |
333 break; | |
334 default: | |
335 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n"); | |
336 return -1; | |
337 } | |
338 | |
339 | |
340 /* Apply PNG filter */ | |
341 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) { | |
342 switch (c->imgtype) { | |
343 case IMGTYPE_YUV111: | |
344 case IMGTYPE_RGB24: | |
345 for (row = 0; row < height; row++) { | |
346 pixel_ptr = row * width * 3; | |
347 yq = encoded[pixel_ptr++]; | |
348 uqvq = encoded[pixel_ptr++]; | |
349 uqvq+=(encoded[pixel_ptr++] << 8); | |
350 for (col = 1; col < width; col++) { | |
351 encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; | |
352 uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8)); | |
353 encoded[pixel_ptr+1] = (uqvq) & 0xff; | |
354 encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff; | |
355 pixel_ptr += 3; | |
356 } | |
357 } | |
358 break; | |
359 case IMGTYPE_YUV422: | |
360 for (row = 0; row < height; row++) { | |
361 pixel_ptr = row * width * 2; | |
362 yq = uq = vq =0; | |
363 for (col = 0; col < width/4; col++) { | |
364 encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; | |
365 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; | |
366 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2]; | |
367 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3]; | |
368 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; | |
369 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5]; | |
370 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6]; | |
371 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7]; | |
372 pixel_ptr += 8; | |
373 } | |
374 } | |
375 break; | |
376 case IMGTYPE_YUV411: | |
377 for (row = 0; row < height; row++) { | |
378 pixel_ptr = row * width / 2 * 3; | |
379 yq = uq = vq =0; | |
380 for (col = 0; col < width/4; col++) { | |
381 encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; | |
382 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; | |
383 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2]; | |
384 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3]; | |
385 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; | |
386 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5]; | |
387 pixel_ptr += 6; | |
388 } | |
389 } | |
390 break; | |
391 case IMGTYPE_YUV211: | |
392 for (row = 0; row < height; row++) { | |
393 pixel_ptr = row * width * 2; | |
394 yq = uq = vq =0; | |
395 for (col = 0; col < width/2; col++) { | |
396 encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; | |
397 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; | |
398 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2]; | |
399 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3]; | |
400 pixel_ptr += 4; | |
401 } | |
402 } | |
403 break; | |
404 case IMGTYPE_YUV420: | |
405 for (row = 0; row < height/2; row++) { | |
406 pixel_ptr = row * width * 3; | |
407 yq = y1q = uq = vq =0; | |
408 for (col = 0; col < width/2; col++) { | |
409 encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; | |
410 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; | |
411 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2]; | |
412 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3]; | |
413 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; | |
414 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5]; | |
415 pixel_ptr += 6; | |
416 } | |
417 } | |
418 break; | |
419 default: | |
420 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n"); | |
421 return -1; | |
422 } | |
423 } | |
424 | |
425 /* Convert colorspace */ | |
426 switch (c->imgtype) { | |
427 case IMGTYPE_YUV111: | |
428 for (row = height - 1; row >= 0; row--) { | |
429 pixel_ptr = row * c->pic.linesize[0]; | |
430 for (col = 0; col < width; col++) { | |
431 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]); | |
432 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]); | |
433 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]); | |
434 encoded += 3; | |
435 } | |
436 } | |
437 break; | |
438 case IMGTYPE_YUV422: | |
439 for (row = height - 1; row >= 0; row--) { | |
440 pixel_ptr = row * c->pic.linesize[0]; | |
441 for (col = 0; col < width/4; col++) { | |
442 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]); | |
443 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]); | |
444 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]); | |
445 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]); | |
446 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]); | |
447 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]); | |
448 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]); | |
449 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]); | |
450 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]); | |
451 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]); | |
452 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]); | |
453 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]); | |
454 encoded += 8; | |
455 } | |
456 } | |
457 break; | |
458 case IMGTYPE_RGB24: | |
459 for (row = height - 1; row >= 0; row--) { | |
460 pixel_ptr = row * c->pic.linesize[0]; | |
461 for (col = 0; col < width; col++) { | |
462 outptr[pixel_ptr++] = encoded[0]; | |
463 outptr[pixel_ptr++] = encoded[1]; | |
464 outptr[pixel_ptr++] = encoded[2]; | |
465 encoded += 3; | |
466 } | |
467 } | |
468 break; | |
469 case IMGTYPE_YUV411: | |
470 for (row = height - 1; row >= 0; row--) { | |
471 pixel_ptr = row * c->pic.linesize[0]; | |
472 for (col = 0; col < width/4; col++) { | |
473 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]); | |
474 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]); | |
475 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]); | |
476 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]); | |
477 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]); | |
478 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]); | |
479 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]); | |
480 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]); | |
481 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]); | |
482 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]); | |
483 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]); | |
484 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]); | |
485 encoded += 6; | |
486 } | |
487 } | |
488 break; | |
489 case IMGTYPE_YUV211: | |
490 for (row = height - 1; row >= 0; row--) { | |
491 pixel_ptr = row * c->pic.linesize[0]; | |
492 for (col = 0; col < width/2; col++) { | |
493 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]); | |
494 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]); | |
495 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]); | |
496 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]); | |
497 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]); | |
498 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]); | |
499 encoded += 4; | |
500 } | |
501 } | |
502 break; | |
503 case IMGTYPE_YUV420: | |
504 for (row = height / 2 - 1; row >= 0; row--) { | |
505 pixel_ptr = 2 * row * c->pic.linesize[0]; | |
506 for (col = 0; col < width/2; col++) { | |
507 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]); | |
508 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]); | |
509 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]); | |
510 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]); | |
511 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]); | |
512 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]); | |
513 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]); | |
514 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]); | |
515 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]); | |
516 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]); | |
517 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]); | |
518 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]); | |
519 pixel_ptr += 6; | |
520 encoded += 6; | |
521 } | |
522 } | |
523 break; | |
524 default: | |
525 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n"); | |
526 return -1; | |
527 } | |
528 | |
529 *data_size = sizeof(AVFrame); | |
530 *(AVFrame*)data = c->pic; | |
531 | |
532 /* always report that the buffer was completely consumed */ | |
533 return buf_size; | |
534 } | |
535 | |
536 | |
537 | |
538 /* | |
539 * | |
540 * Encode a frame | |
541 * | |
542 */ | |
543 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){ | |
544 LclContext *c = avctx->priv_data; | |
545 AVFrame *pict = data; | |
546 AVFrame * const p = &c->pic; | |
547 int i; | |
548 int zret; // Zlib return code | |
549 | |
550 #ifndef CONFIG_ZLIB | |
551 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n"); | |
552 return -1; | |
553 #else | |
554 | |
555 init_put_bits(&c->pb, buf, buf_size); | |
556 | |
557 *p = *pict; | |
558 p->pict_type= FF_I_TYPE; | |
559 p->key_frame= 1; | |
560 | |
561 if(avctx->pix_fmt != PIX_FMT_BGR24){ | |
562 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n"); | |
563 return -1; | |
564 } | |
565 | |
566 zret = deflateReset(&(c->zstream)); | |
567 if (zret != Z_OK) { | |
568 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret); | |
569 return -1; | |
570 } | |
571 c->zstream.next_out = c->comp_buf; | |
572 c->zstream.avail_out = c->max_comp_size; | |
573 | |
2250
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
574 for(i = avctx->height - 1; i >= 0; i--) { |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
575 c->zstream.next_in = p->data[0]+p->linesize[0]*i; |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
576 c->zstream.avail_in = avctx->width*3; |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
577 zret = deflate(&(c->zstream), Z_NO_FLUSH); |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
578 if (zret != Z_OK) { |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
579 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
580 return -1; |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
581 } |
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
582 } |
1743 | 583 zret = deflate(&(c->zstream), Z_FINISH); |
2250
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
584 if (zret != Z_STREAM_END) { |
1743 | 585 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret); |
586 return -1; | |
587 } | |
588 | |
589 for (i = 0; i < c->zstream.total_out; i++) | |
590 put_bits(&c->pb, 8, c->comp_buf[i]); | |
591 flush_put_bits(&c->pb); | |
592 | |
593 return c->zstream.total_out; | |
594 #endif | |
595 } | |
596 | |
597 | |
598 | |
599 /* | |
600 * | |
601 * Init lcl decoder | |
602 * | |
603 */ | |
604 static int decode_init(AVCodecContext *avctx) | |
605 { | |
606 LclContext * const c = (LclContext *)avctx->priv_data; | |
607 int basesize = avctx->width * avctx->height; | |
608 int zret; // Zlib return code | |
609 | |
610 c->avctx = avctx; | |
611 avctx->has_b_frames = 0; | |
612 | |
613 c->pic.data[0] = NULL; | |
614 | |
615 #ifdef CONFIG_ZLIB | |
616 // Needed if zlib unused or init aborted before inflateInit | |
617 memset(&(c->zstream), 0, sizeof(z_stream)); | |
618 #endif | |
619 | |
620 if (avctx->extradata_size < 8) { | |
621 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n"); | |
622 return 1; | |
623 } | |
624 | |
625 /* Check codec type */ | |
626 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) || | |
627 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) { | |
628 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n"); | |
629 } | |
630 | |
631 /* Detect image type */ | |
632 switch (c->imgtype = *((char *)avctx->extradata + 4)) { | |
633 case IMGTYPE_YUV111: | |
634 c->decomp_size = basesize * 3; | |
635 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n"); | |
636 break; | |
637 case IMGTYPE_YUV422: | |
638 c->decomp_size = basesize * 2; | |
639 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n"); | |
640 break; | |
641 case IMGTYPE_RGB24: | |
642 c->decomp_size = basesize * 3; | |
643 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n"); | |
644 break; | |
645 case IMGTYPE_YUV411: | |
646 c->decomp_size = basesize / 2 * 3; | |
647 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n"); | |
648 break; | |
649 case IMGTYPE_YUV211: | |
650 c->decomp_size = basesize * 2; | |
651 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n"); | |
652 break; | |
653 case IMGTYPE_YUV420: | |
654 c->decomp_size = basesize / 2 * 3; | |
655 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n"); | |
656 break; | |
657 default: | |
658 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype); | |
659 return 1; | |
660 } | |
661 | |
662 /* Detect compression method */ | |
663 c->compression = *((char *)avctx->extradata + 5); | |
664 switch (avctx->codec_id) { | |
665 case CODEC_ID_MSZH: | |
666 switch (c->compression) { | |
667 case COMP_MSZH: | |
668 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n"); | |
669 break; | |
670 case COMP_MSZH_NOCOMP: | |
671 c->decomp_size = 0; | |
672 av_log(avctx, AV_LOG_INFO, "No compression.\n"); | |
673 break; | |
674 default: | |
675 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression); | |
676 return 1; | |
677 } | |
678 break; | |
679 case CODEC_ID_ZLIB: | |
680 #ifdef CONFIG_ZLIB | |
681 switch (c->compression) { | |
682 case COMP_ZLIB_HISPEED: | |
683 av_log(avctx, AV_LOG_INFO, "High speed compression.\n"); | |
684 break; | |
685 case COMP_ZLIB_HICOMP: | |
686 av_log(avctx, AV_LOG_INFO, "High compression.\n"); | |
687 break; | |
688 case COMP_ZLIB_NORMAL: | |
689 av_log(avctx, AV_LOG_INFO, "Normal compression.\n"); | |
690 break; | |
691 default: | |
692 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) { | |
693 av_log(avctx, AV_LOG_ERROR, "Unusupported compression level for ZLIB: (%d).\n", c->compression); | |
694 return 1; | |
695 } | |
696 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression); | |
697 } | |
698 #else | |
699 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n"); | |
700 return 1; | |
701 #endif | |
702 break; | |
703 default: | |
704 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n"); | |
705 return 1; | |
706 } | |
707 | |
708 /* Allocate decompression buffer */ | |
709 /* 4*8 max overflow space for mszh decomp algorithm */ | |
710 if (c->decomp_size) { | |
711 if ((c->decomp_buf = av_malloc(c->decomp_size+4*8)) == NULL) { | |
712 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); | |
713 return 1; | |
714 } | |
715 } | |
716 | |
717 /* Detect flags */ | |
718 c->flags = *((char *)avctx->extradata + 6); | |
719 if (c->flags & FLAG_MULTITHREAD) | |
720 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n"); | |
721 if (c->flags & FLAG_NULLFRAME) | |
722 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n"); | |
723 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) | |
724 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n"); | |
725 if (c->flags & FLAGMASK_UNUSED) | |
726 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags); | |
727 | |
728 /* If needed init zlib */ | |
729 if (avctx->codec_id == CODEC_ID_ZLIB) { | |
730 #ifdef CONFIG_ZLIB | |
731 c->zstream.zalloc = Z_NULL; | |
732 c->zstream.zfree = Z_NULL; | |
733 c->zstream.opaque = Z_NULL; | |
734 zret = inflateInit(&(c->zstream)); | |
735 if (zret != Z_OK) { | |
736 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); | |
737 return 1; | |
738 } | |
739 #else | |
740 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n"); | |
741 return 1; | |
742 #endif | |
743 } | |
744 | |
745 avctx->pix_fmt = PIX_FMT_BGR24; | |
746 | |
747 return 0; | |
748 } | |
749 | |
750 | |
751 | |
752 /* | |
753 * | |
754 * Init lcl encoder | |
755 * | |
756 */ | |
757 static int encode_init(AVCodecContext *avctx) | |
758 { | |
759 LclContext *c = avctx->priv_data; | |
760 int zret; // Zlib return code | |
761 | |
762 #ifndef CONFIG_ZLIB | |
763 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n"); | |
764 return 1; | |
765 #else | |
766 | |
767 c->avctx= avctx; | |
768 | |
769 assert(avctx->width && avctx->height); | |
770 | |
771 avctx->extradata= av_mallocz(8); | |
772 avctx->coded_frame= &c->pic; | |
773 | |
774 // Will be user settable someday | |
775 c->compression = 6; | |
776 c->flags = 0; | |
777 | |
778 switch(avctx->pix_fmt){ | |
779 case PIX_FMT_BGR24: | |
780 c->imgtype = IMGTYPE_RGB24; | |
781 c->decomp_size = avctx->width * avctx->height * 3; | |
782 avctx->bits_per_sample= 24; | |
783 break; | |
784 default: | |
785 av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt); | |
786 return -1; | |
787 } | |
788 | |
789 ((uint8_t*)avctx->extradata)[0]= 4; | |
790 ((uint8_t*)avctx->extradata)[1]= 0; | |
791 ((uint8_t*)avctx->extradata)[2]= 0; | |
792 ((uint8_t*)avctx->extradata)[3]= 0; | |
793 ((uint8_t*)avctx->extradata)[4]= c->imgtype; | |
794 ((uint8_t*)avctx->extradata)[5]= c->compression; | |
795 ((uint8_t*)avctx->extradata)[6]= c->flags; | |
2250
902caf560c43
Zlib encoder: fix image orientation (was flipped), 100l in deflate error
rtognimp
parents:
2248
diff
changeset
|
796 ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB; |
1743 | 797 c->avctx->extradata_size= 8; |
798 | |
799 c->zstream.zalloc = Z_NULL; | |
800 c->zstream.zfree = Z_NULL; | |
801 c->zstream.opaque = Z_NULL; | |
802 zret = deflateInit(&(c->zstream), c->compression); | |
803 if (zret != Z_OK) { | |
804 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret); | |
805 return 1; | |
806 } | |
807 | |
808 /* Conservative upper bound taken from zlib v1.2.1 source */ | |
809 c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) + | |
810 ((c->decomp_size + 63) >> 6) + 11; | |
811 if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) { | |
812 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n"); | |
813 return 1; | |
814 } | |
815 | |
816 return 0; | |
817 #endif | |
818 } | |
819 | |
820 | |
821 | |
822 | |
823 | |
824 /* | |
825 * | |
826 * Uninit lcl decoder | |
827 * | |
828 */ | |
829 static int decode_end(AVCodecContext *avctx) | |
830 { | |
831 LclContext * const c = (LclContext *)avctx->priv_data; | |
832 | |
833 if (c->pic.data[0]) | |
834 avctx->release_buffer(avctx, &c->pic); | |
835 #ifdef CONFIG_ZLIB | |
836 inflateEnd(&(c->zstream)); | |
837 #endif | |
838 | |
839 return 0; | |
840 } | |
841 | |
842 | |
843 | |
844 /* | |
845 * | |
846 * Uninit lcl encoder | |
847 * | |
848 */ | |
849 static int encode_end(AVCodecContext *avctx) | |
850 { | |
851 LclContext *c = avctx->priv_data; | |
852 | |
853 av_freep(&avctx->extradata); | |
2248
e4e1b4f31db6
segfault fix by (Kostya <cannonball at bw-team dot com>)
michael
parents:
1744
diff
changeset
|
854 av_freep(&c->comp_buf); |
1743 | 855 #ifdef CONFIG_ZLIB |
856 deflateEnd(&(c->zstream)); | |
857 #endif | |
858 | |
859 return 0; | |
860 } | |
861 | |
862 AVCodec mszh_decoder = { | |
863 "mszh", | |
864 CODEC_TYPE_VIDEO, | |
865 CODEC_ID_MSZH, | |
866 sizeof(LclContext), | |
867 decode_init, | |
868 NULL, | |
869 decode_end, | |
870 decode_frame, | |
871 CODEC_CAP_DR1, | |
872 }; | |
873 | |
874 | |
875 AVCodec zlib_decoder = { | |
876 "zlib", | |
877 CODEC_TYPE_VIDEO, | |
878 CODEC_ID_ZLIB, | |
879 sizeof(LclContext), | |
880 decode_init, | |
881 NULL, | |
882 decode_end, | |
883 decode_frame, | |
884 CODEC_CAP_DR1, | |
885 }; | |
886 | |
887 #ifdef CONFIG_ENCODERS | |
888 | |
889 AVCodec zlib_encoder = { | |
890 "zlib", | |
891 CODEC_TYPE_VIDEO, | |
892 CODEC_ID_ZLIB, | |
893 sizeof(LclContext), | |
894 encode_init, | |
895 encode_frame, | |
896 encode_end, | |
897 // .options = lcl_options, | |
898 }; | |
899 | |
900 #endif //CONFIG_ENCODERS |