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