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