Mercurial > libavcodec.hg
annotate lcldec.c @ 9422:997f587c3db8 libavcodec
Fix wrong size computation for buffer. Patch is part of
netgem's changeset.
author | reynaldo |
---|---|
date | Sun, 12 Apr 2009 21:48:43 +0000 |
parents | 54bc8a2727b0 |
children | 0dce4fe6e6f3 |
rev | line source |
---|---|
1743 | 1 /* |
2 * LCL (LossLess Codec Library) Codec | |
3 * Copyright (c) 2002-2004 Roberto Togni | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
1743 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
1743 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
1743 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3777
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1743 | 20 */ |
21 | |
22 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8673
diff
changeset
|
23 * @file libavcodec/lcldec.c |
1743 | 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 | |
4962
f99e40a7155b
Remove redundant #inclusion of common.h, avcodec.h already #includes it.
diego
parents:
4827
diff
changeset
|
44 #include "avcodec.h" |
2398
582e635cfa08
common.c -> bitstream.c (and the single non bitstream func -> utils.c)
michael
parents:
2250
diff
changeset
|
45 #include "bitstream.h" |
5294 | 46 #include "lcl.h" |
1743 | 47 |
8590 | 48 #if CONFIG_ZLIB |
1743 | 49 #include <zlib.h> |
50 #endif | |
51 | |
52 /* | |
53 * Decoder context | |
54 */ | |
5294 | 55 typedef struct LclDecContext { |
5297 | 56 AVFrame pic; |
1743 | 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; | |
8590 | 68 #if CONFIG_ZLIB |
1743 | 69 z_stream zstream; |
70 #endif | |
5294 | 71 } LclDecContext; |
1743 | 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 | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
114 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize) |
1743 | 115 { |
116 unsigned char *destptr_bak = destptr; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
117 unsigned char *destptr_end = destptr + destsize; |
1743 | 118 unsigned char mask = 0; |
119 unsigned char maskbit = 0; | |
120 unsigned int ofs, cnt; | |
2967 | 121 |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
122 while ((srclen > 0) && (destptr < destptr_end)) { |
1743 | 123 if (maskbit == 0) { |
124 mask = *(srcptr++); | |
125 maskbit = 8; | |
126 srclen--; | |
127 continue; | |
128 } | |
129 if ((mask & (1 << (--maskbit))) == 0) { | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
130 if (destptr + 4 > destptr_end) |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
131 break; |
1743 | 132 *(int*)destptr = *(int*)srcptr; |
133 srclen -= 4; | |
134 destptr += 4; | |
135 srcptr += 4; | |
136 } else { | |
137 ofs = *(srcptr++); | |
138 cnt = *(srcptr++); | |
6375 | 139 ofs += cnt * 256; |
1743 | 140 cnt = ((cnt >> 3) & 0x1f) + 1; |
141 ofs &= 0x7ff; | |
142 srclen -= 2; | |
143 cnt *= 4; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
144 if (destptr + cnt > destptr_end) { |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
145 cnt = destptr_end - destptr; |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
146 } |
1743 | 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 */ | |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
164 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) |
1743 | 165 { |
9355
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
166 const uint8_t *buf = avpkt->data; |
54bc8a2727b0
Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents:
8718
diff
changeset
|
167 int buf_size = avpkt->size; |
5297 | 168 LclDecContext * const c = avctx->priv_data; |
169 unsigned char *encoded = (unsigned char *)buf; | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
170 unsigned int pixel_ptr; |
1743 | 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; | |
8590 | 179 #if CONFIG_ZLIB |
1743 | 180 int zret; // Zlib return code |
181 #endif | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
182 unsigned int len = buf_size; |
1743 | 183 |
5297 | 184 if(c->pic.data[0]) |
185 avctx->release_buffer(avctx, &c->pic); | |
1743 | 186 |
5297 | 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 } | |
1743 | 193 |
194 outptr = c->pic.data[0]; // Output image pointer | |
195 | |
196 /* Decompress frame */ | |
197 switch (avctx->codec_id) { | |
5297 | 198 case CODEC_ID_MSZH: |
199 switch (c->compression) { | |
200 case COMP_MSZH: | |
1743 | 201 if (c->flags & FLAG_MULTITHREAD) { |
202 mthread_inlen = *((unsigned int*)encoded); | |
203 mthread_outlen = *((unsigned int*)(encoded+4)); | |
5297 | 204 if (mthread_outlen > c->decomp_size) // this should not happen |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
205 mthread_outlen = c->decomp_size; |
5297 | 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); | |
1743 | 210 return -1; |
211 } | |
5297 | 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); | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
217 return -1; |
1743 | 218 } |
5297 | 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); | |
1743 | 226 return -1; |
227 } | |
5297 | 228 encoded = c->decomp_buf; |
229 len = mszh_dlen; | |
1743 | 230 } |
5297 | 231 break; |
232 case COMP_MSZH_NOCOMP: | |
1743 | 233 break; |
234 default: | |
5297 | 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: | |
8590 | 240 #if CONFIG_ZLIB |
5297 | 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); | |
1743 | 250 return -1; |
5297 | 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; | |
6375 | 307 len = c->decomp_size; |
5297 | 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; | |
1743 | 316 } |
317 | |
318 | |
319 /* Apply PNG filter */ | |
320 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) { | |
321 switch (c->imgtype) { | |
5297 | 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; | |
1743 | 334 } |
5297 | 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; | |
1743 | 351 } |
5297 | 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; | |
1743 | 366 } |
5297 | 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; | |
1743 | 379 } |
5297 | 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; | |
1743 | 394 } |
5297 | 395 } |
396 break; | |
397 default: | |
398 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n"); | |
399 return -1; | |
1743 | 400 } |
401 } | |
402 | |
403 /* Convert colorspace */ | |
404 switch (c->imgtype) { | |
5297 | 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; | |
1743 | 413 } |
5297 | 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; | |
1743 | 433 } |
5297 | 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; | |
1743 | 444 } |
5297 | 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; | |
1743 | 464 } |
5297 | 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; | |
1743 | 478 } |
5297 | 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; | |
1743 | 499 } |
5297 | 500 } |
501 break; | |
502 default: | |
503 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n"); | |
504 return -1; | |
1743 | 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 */ | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6375
diff
changeset
|
519 static av_cold int decode_init(AVCodecContext *avctx) |
1743 | 520 { |
5294 | 521 LclDecContext * const c = avctx->priv_data; |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
522 unsigned int basesize = avctx->width * avctx->height; |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
523 unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3); |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
524 unsigned int max_decomp_size; |
1743 | 525 int zret; // Zlib return code |
526 | |
527 c->pic.data[0] = NULL; | |
528 | |
8590 | 529 #if CONFIG_ZLIB |
1743 | 530 // Needed if zlib unused or init aborted before inflateInit |
2967 | 531 memset(&(c->zstream), 0, sizeof(z_stream)); |
1743 | 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 | |
2429
4b350cc506a7
Use avcodec_check_dimensions instead of custom hack
rtognimp
parents:
2418
diff
changeset
|
539 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) { |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
540 return 1; |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
541 } |
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
542 |
2967 | 543 /* Check codec type */ |
1743 | 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)) { | |
5297 | 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; | |
1743 | 584 } |
585 | |
586 /* Detect compression method */ | |
587 c->compression = *((char *)avctx->extradata + 5); | |
588 switch (avctx->codec_id) { | |
5297 | 589 case CODEC_ID_MSZH: |
590 switch (c->compression) { | |
591 case COMP_MSZH: | |
592 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n"); | |
1743 | 593 break; |
5297 | 594 case COMP_MSZH_NOCOMP: |
595 c->decomp_size = 0; | |
596 av_log(avctx, AV_LOG_INFO, "No compression.\n"); | |
1743 | 597 break; |
598 default: | |
5297 | 599 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression); |
1743 | 600 return 1; |
5297 | 601 } |
602 break; | |
603 case CODEC_ID_ZLIB: | |
8590 | 604 #if CONFIG_ZLIB |
5297 | 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; | |
1743 | 630 } |
631 | |
632 /* Allocate decompression buffer */ | |
633 if (c->decomp_size) { | |
2418
82af834636c2
Check pointers before writing to memory, fix possible integer overflows
rtognimp
parents:
2398
diff
changeset
|
634 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) { |
1743 | 635 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); |
636 return 1; | |
637 } | |
638 } | |
2967 | 639 |
640 /* Detect flags */ | |
1743 | 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) { | |
8590 | 653 #if CONFIG_ZLIB |
1743 | 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 | |
5297 | 663 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n"); |
664 return 1; | |
1743 | 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 */ | |
6517
48759bfbd073
Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents:
6375
diff
changeset
|
678 static av_cold int decode_end(AVCodecContext *avctx) |
1743 | 679 { |
5297 | 680 LclDecContext * const c = avctx->priv_data; |
1743 | 681 |
5297 | 682 if (c->pic.data[0]) |
683 avctx->release_buffer(avctx, &c->pic); | |
8590 | 684 #if CONFIG_ZLIB |
1743 | 685 inflateEnd(&(c->zstream)); |
686 #endif | |
687 | |
5297 | 688 return 0; |
1743 | 689 } |
690 | |
8590 | 691 #if CONFIG_MSZH_DECODER |
1743 | 692 AVCodec mszh_decoder = { |
5297 | 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, | |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6712
diff
changeset
|
702 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"), |
1743 | 703 }; |
3777 | 704 #endif |
1743 | 705 |
8590 | 706 #if CONFIG_ZLIB_DECODER |
1743 | 707 AVCodec zlib_decoder = { |
5297 | 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, | |
7040
e943e1409077
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents:
6712
diff
changeset
|
717 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"), |
1743 | 718 }; |
3777 | 719 #endif |