| 1 | /* |
| 2 | * LCL (LossLess Codec Library) Codec |
| 3 | * Copyright (c) 2002-2004 Roberto Togni |
| 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file |
| 24 | * LCL (LossLess Codec Library) Video Codec |
| 25 | * Decoder for MSZH and ZLIB codecs |
| 26 | * Experimental encoder for ZLIB RGB24 |
| 27 | * |
| 28 | * Fourcc: MSZH, ZLIB |
| 29 | * |
| 30 | * Original Win32 dll: |
| 31 | * Ver2.23 By Kenji Oshima 2000.09.20 |
| 32 | * avimszh.dll, avizlib.dll |
| 33 | * |
| 34 | * A description of the decoding algorithm can be found here: |
| 35 | * http://www.pcisys.net/~melanson/codecs |
| 36 | * |
| 37 | * Supports: BGR24 (RGB 24bpp) |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | |
| 44 | #include "libavutil/mem.h" |
| 45 | #include "libavutil/pixdesc.h" |
| 46 | #include "avcodec.h" |
| 47 | #include "bytestream.h" |
| 48 | #include "internal.h" |
| 49 | #include "lcl.h" |
| 50 | |
| 51 | #if CONFIG_ZLIB_DECODER |
| 52 | #include <zlib.h> |
| 53 | #endif |
| 54 | |
| 55 | /* |
| 56 | * Decoder context |
| 57 | */ |
| 58 | typedef struct LclDecContext { |
| 59 | // Image type |
| 60 | int imgtype; |
| 61 | // Compression type |
| 62 | int compression; |
| 63 | // Flags |
| 64 | int flags; |
| 65 | // Decompressed data size |
| 66 | unsigned int decomp_size; |
| 67 | // Decompression buffer |
| 68 | unsigned char* decomp_buf; |
| 69 | #if CONFIG_ZLIB_DECODER |
| 70 | z_stream zstream; |
| 71 | #endif |
| 72 | } LclDecContext; |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes |
| 77 | * @param destptr must be padded sufficiently for av_memcpy_backptr |
| 78 | */ |
| 79 | static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize) |
| 80 | { |
| 81 | unsigned char *destptr_bak = destptr; |
| 82 | unsigned char *destptr_end = destptr + destsize; |
| 83 | const unsigned char *srcptr_end = srcptr + srclen; |
| 84 | unsigned mask = *srcptr++; |
| 85 | unsigned maskbit = 0x80; |
| 86 | |
| 87 | while (srcptr < srcptr_end && destptr < destptr_end) { |
| 88 | if (!(mask & maskbit)) { |
| 89 | memcpy(destptr, srcptr, 4); |
| 90 | destptr += 4; |
| 91 | srcptr += 4; |
| 92 | } else { |
| 93 | unsigned ofs = bytestream_get_le16(&srcptr); |
| 94 | unsigned cnt = (ofs >> 11) + 1; |
| 95 | ofs &= 0x7ff; |
| 96 | ofs = FFMIN(ofs, destptr - destptr_bak); |
| 97 | cnt *= 4; |
| 98 | cnt = FFMIN(cnt, destptr_end - destptr); |
| 99 | if (ofs) { |
| 100 | av_memcpy_backptr(destptr, ofs, cnt); |
| 101 | } else { |
| 102 | // Not known what the correct behaviour is, but |
| 103 | // this at least avoids uninitialized data. |
| 104 | memset(destptr, 0, cnt); |
| 105 | } |
| 106 | destptr += cnt; |
| 107 | } |
| 108 | maskbit >>= 1; |
| 109 | if (!maskbit) { |
| 110 | mask = *srcptr++; |
| 111 | while (!mask) { |
| 112 | if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break; |
| 113 | memcpy(destptr, srcptr, 32); |
| 114 | destptr += 32; |
| 115 | srcptr += 32; |
| 116 | mask = *srcptr++; |
| 117 | } |
| 118 | maskbit = 0x80; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return destptr - destptr_bak; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | #if CONFIG_ZLIB_DECODER |
| 127 | /** |
| 128 | * @brief decompress a zlib-compressed data block into decomp_buf |
| 129 | * @param src compressed input buffer |
| 130 | * @param src_len data length in input buffer |
| 131 | * @param offset offset in decomp_buf |
| 132 | * @param expected expected decompressed length |
| 133 | */ |
| 134 | static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected) |
| 135 | { |
| 136 | LclDecContext *c = avctx->priv_data; |
| 137 | int zret = inflateReset(&c->zstream); |
| 138 | if (zret != Z_OK) { |
| 139 | av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret); |
| 140 | return AVERROR_UNKNOWN; |
| 141 | } |
| 142 | c->zstream.next_in = (uint8_t *)src; |
| 143 | c->zstream.avail_in = src_len; |
| 144 | c->zstream.next_out = c->decomp_buf + offset; |
| 145 | c->zstream.avail_out = c->decomp_size - offset; |
| 146 | zret = inflate(&c->zstream, Z_FINISH); |
| 147 | if (zret != Z_OK && zret != Z_STREAM_END) { |
| 148 | av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret); |
| 149 | return AVERROR_UNKNOWN; |
| 150 | } |
| 151 | if (expected != (unsigned int)c->zstream.total_out) { |
| 152 | av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n", |
| 153 | expected, c->zstream.total_out); |
| 154 | return AVERROR_UNKNOWN; |
| 155 | } |
| 156 | return c->zstream.total_out; |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | |
| 161 | /* |
| 162 | * |
| 163 | * Decode a frame |
| 164 | * |
| 165 | */ |
| 166 | static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt) |
| 167 | { |
| 168 | AVFrame *frame = data; |
| 169 | const uint8_t *buf = avpkt->data; |
| 170 | int buf_size = avpkt->size; |
| 171 | LclDecContext * const c = avctx->priv_data; |
| 172 | unsigned int pixel_ptr; |
| 173 | int row, col; |
| 174 | unsigned char *encoded = avpkt->data, *outptr; |
| 175 | uint8_t *y_out, *u_out, *v_out; |
| 176 | unsigned int width = avctx->width; // Real image width |
| 177 | unsigned int height = avctx->height; // Real image height |
| 178 | unsigned int mszh_dlen; |
| 179 | unsigned char yq, y1q, uq, vq; |
| 180 | int uqvq, ret; |
| 181 | unsigned int mthread_inlen, mthread_outlen; |
| 182 | unsigned int len = buf_size; |
| 183 | |
| 184 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 185 | return ret; |
| 186 | |
| 187 | outptr = frame->data[0]; // Output image pointer |
| 188 | |
| 189 | /* Decompress frame */ |
| 190 | switch (avctx->codec_id) { |
| 191 | case AV_CODEC_ID_MSZH: |
| 192 | switch (c->compression) { |
| 193 | case COMP_MSZH: |
| 194 | if (c->imgtype == IMGTYPE_RGB24 && len == width * height * 3 || |
| 195 | c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) { |
| 196 | ; |
| 197 | } else if (c->flags & FLAG_MULTITHREAD) { |
| 198 | mthread_inlen = AV_RL32(buf); |
| 199 | if (len < 8) { |
| 200 | av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len); |
| 201 | return AVERROR_INVALIDDATA; |
| 202 | } |
| 203 | mthread_inlen = FFMIN(mthread_inlen, len - 8); |
| 204 | mthread_outlen = AV_RL32(buf + 4); |
| 205 | mthread_outlen = FFMIN(mthread_outlen, c->decomp_size); |
| 206 | mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size); |
| 207 | if (mthread_outlen != mszh_dlen) { |
| 208 | av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n", |
| 209 | mthread_outlen, mszh_dlen); |
| 210 | return AVERROR_INVALIDDATA; |
| 211 | } |
| 212 | mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen, |
| 213 | c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen); |
| 214 | if (mthread_outlen != mszh_dlen) { |
| 215 | av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n", |
| 216 | mthread_outlen, mszh_dlen); |
| 217 | return AVERROR_INVALIDDATA; |
| 218 | } |
| 219 | encoded = c->decomp_buf; |
| 220 | len = c->decomp_size; |
| 221 | } else { |
| 222 | mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size); |
| 223 | if (c->decomp_size != mszh_dlen) { |
| 224 | av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n", |
| 225 | c->decomp_size, mszh_dlen); |
| 226 | return AVERROR_INVALIDDATA; |
| 227 | } |
| 228 | encoded = c->decomp_buf; |
| 229 | len = mszh_dlen; |
| 230 | } |
| 231 | break; |
| 232 | case COMP_MSZH_NOCOMP: { |
| 233 | int bppx2; |
| 234 | switch (c->imgtype) { |
| 235 | case IMGTYPE_YUV111: |
| 236 | case IMGTYPE_RGB24: |
| 237 | bppx2 = 6; |
| 238 | break; |
| 239 | case IMGTYPE_YUV422: |
| 240 | case IMGTYPE_YUV211: |
| 241 | bppx2 = 4; |
| 242 | break; |
| 243 | case IMGTYPE_YUV411: |
| 244 | case IMGTYPE_YUV420: |
| 245 | bppx2 = 3; |
| 246 | break; |
| 247 | default: |
| 248 | bppx2 = 0; // will error out below |
| 249 | break; |
| 250 | } |
| 251 | if (len < ((width * height * bppx2) >> 1)) |
| 252 | return AVERROR_INVALIDDATA; |
| 253 | break; |
| 254 | } |
| 255 | default: |
| 256 | av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n"); |
| 257 | return AVERROR_INVALIDDATA; |
| 258 | } |
| 259 | break; |
| 260 | #if CONFIG_ZLIB_DECODER |
| 261 | case AV_CODEC_ID_ZLIB: |
| 262 | /* Using the original dll with normal compression (-1) and RGB format |
| 263 | * gives a file with ZLIB fourcc, but frame is really uncompressed. |
| 264 | * To be sure that's true check also frame size */ |
| 265 | if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 && |
| 266 | len == width * height * 3) { |
| 267 | if (c->flags & FLAG_PNGFILTER) { |
| 268 | memcpy(c->decomp_buf, buf, len); |
| 269 | encoded = c->decomp_buf; |
| 270 | } else { |
| 271 | break; |
| 272 | } |
| 273 | } else if (c->flags & FLAG_MULTITHREAD) { |
| 274 | mthread_inlen = AV_RL32(buf); |
| 275 | mthread_inlen = FFMIN(mthread_inlen, len - 8); |
| 276 | mthread_outlen = AV_RL32(buf + 4); |
| 277 | mthread_outlen = FFMIN(mthread_outlen, c->decomp_size); |
| 278 | ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen); |
| 279 | if (ret < 0) return ret; |
| 280 | ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen, |
| 281 | mthread_outlen, mthread_outlen); |
| 282 | if (ret < 0) return ret; |
| 283 | } else { |
| 284 | int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size); |
| 285 | if (ret < 0) return ret; |
| 286 | } |
| 287 | encoded = c->decomp_buf; |
| 288 | len = c->decomp_size; |
| 289 | break; |
| 290 | #endif |
| 291 | default: |
| 292 | av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n"); |
| 293 | return AVERROR_INVALIDDATA; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /* Apply PNG filter */ |
| 298 | if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) { |
| 299 | switch (c->imgtype) { |
| 300 | case IMGTYPE_YUV111: |
| 301 | case IMGTYPE_RGB24: |
| 302 | for (row = 0; row < height; row++) { |
| 303 | pixel_ptr = row * width * 3; |
| 304 | yq = encoded[pixel_ptr++]; |
| 305 | uqvq = AV_RL16(encoded+pixel_ptr); |
| 306 | pixel_ptr += 2; |
| 307 | for (col = 1; col < width; col++) { |
| 308 | encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
| 309 | uqvq -= AV_RL16(encoded+pixel_ptr+1); |
| 310 | AV_WL16(encoded+pixel_ptr+1, uqvq); |
| 311 | pixel_ptr += 3; |
| 312 | } |
| 313 | } |
| 314 | break; |
| 315 | case IMGTYPE_YUV422: |
| 316 | for (row = 0; row < height; row++) { |
| 317 | pixel_ptr = row * width * 2; |
| 318 | yq = uq = vq =0; |
| 319 | for (col = 0; col < width/4; col++) { |
| 320 | encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
| 321 | encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
| 322 | encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2]; |
| 323 | encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3]; |
| 324 | encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; |
| 325 | encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5]; |
| 326 | encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6]; |
| 327 | encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7]; |
| 328 | pixel_ptr += 8; |
| 329 | } |
| 330 | } |
| 331 | break; |
| 332 | case IMGTYPE_YUV411: |
| 333 | for (row = 0; row < height; row++) { |
| 334 | pixel_ptr = row * width / 2 * 3; |
| 335 | yq = uq = vq =0; |
| 336 | for (col = 0; col < width/4; col++) { |
| 337 | encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
| 338 | encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
| 339 | encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2]; |
| 340 | encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3]; |
| 341 | encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; |
| 342 | encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5]; |
| 343 | pixel_ptr += 6; |
| 344 | } |
| 345 | } |
| 346 | break; |
| 347 | case IMGTYPE_YUV211: |
| 348 | for (row = 0; row < height; row++) { |
| 349 | pixel_ptr = row * width * 2; |
| 350 | yq = uq = vq =0; |
| 351 | for (col = 0; col < width/2; col++) { |
| 352 | encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
| 353 | encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
| 354 | encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2]; |
| 355 | encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3]; |
| 356 | pixel_ptr += 4; |
| 357 | } |
| 358 | } |
| 359 | break; |
| 360 | case IMGTYPE_YUV420: |
| 361 | for (row = 0; row < height/2; row++) { |
| 362 | pixel_ptr = row * width * 3; |
| 363 | yq = y1q = uq = vq =0; |
| 364 | for (col = 0; col < width/2; col++) { |
| 365 | encoded[pixel_ptr] = yq -= encoded[pixel_ptr]; |
| 366 | encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1]; |
| 367 | encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2]; |
| 368 | encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3]; |
| 369 | encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4]; |
| 370 | encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5]; |
| 371 | pixel_ptr += 6; |
| 372 | } |
| 373 | } |
| 374 | break; |
| 375 | default: |
| 376 | av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n"); |
| 377 | return AVERROR_INVALIDDATA; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* Convert colorspace */ |
| 382 | y_out = frame->data[0] + (height - 1) * frame->linesize[0]; |
| 383 | u_out = frame->data[1] + (height - 1) * frame->linesize[1]; |
| 384 | v_out = frame->data[2] + (height - 1) * frame->linesize[2]; |
| 385 | switch (c->imgtype) { |
| 386 | case IMGTYPE_YUV111: |
| 387 | for (row = 0; row < height; row++) { |
| 388 | for (col = 0; col < width; col++) { |
| 389 | y_out[col] = *encoded++; |
| 390 | u_out[col] = *encoded++ + 128; |
| 391 | v_out[col] = *encoded++ + 128; |
| 392 | } |
| 393 | y_out -= frame->linesize[0]; |
| 394 | u_out -= frame->linesize[1]; |
| 395 | v_out -= frame->linesize[2]; |
| 396 | } |
| 397 | break; |
| 398 | case IMGTYPE_YUV422: |
| 399 | for (row = 0; row < height; row++) { |
| 400 | for (col = 0; col < width - 3; col += 4) { |
| 401 | memcpy(y_out + col, encoded, 4); |
| 402 | encoded += 4; |
| 403 | u_out[ col >> 1 ] = *encoded++ + 128; |
| 404 | u_out[(col >> 1) + 1] = *encoded++ + 128; |
| 405 | v_out[ col >> 1 ] = *encoded++ + 128; |
| 406 | v_out[(col >> 1) + 1] = *encoded++ + 128; |
| 407 | } |
| 408 | y_out -= frame->linesize[0]; |
| 409 | u_out -= frame->linesize[1]; |
| 410 | v_out -= frame->linesize[2]; |
| 411 | } |
| 412 | break; |
| 413 | case IMGTYPE_RGB24: |
| 414 | for (row = height - 1; row >= 0; row--) { |
| 415 | pixel_ptr = row * frame->linesize[0]; |
| 416 | memcpy(outptr + pixel_ptr, encoded, 3 * width); |
| 417 | encoded += 3 * width; |
| 418 | } |
| 419 | break; |
| 420 | case IMGTYPE_YUV411: |
| 421 | for (row = 0; row < height; row++) { |
| 422 | for (col = 0; col < width - 3; col += 4) { |
| 423 | memcpy(y_out + col, encoded, 4); |
| 424 | encoded += 4; |
| 425 | u_out[col >> 2] = *encoded++ + 128; |
| 426 | v_out[col >> 2] = *encoded++ + 128; |
| 427 | } |
| 428 | y_out -= frame->linesize[0]; |
| 429 | u_out -= frame->linesize[1]; |
| 430 | v_out -= frame->linesize[2]; |
| 431 | } |
| 432 | break; |
| 433 | case IMGTYPE_YUV211: |
| 434 | for (row = 0; row < height; row++) { |
| 435 | for (col = 0; col < width - 1; col += 2) { |
| 436 | memcpy(y_out + col, encoded, 2); |
| 437 | encoded += 2; |
| 438 | u_out[col >> 1] = *encoded++ + 128; |
| 439 | v_out[col >> 1] = *encoded++ + 128; |
| 440 | } |
| 441 | y_out -= frame->linesize[0]; |
| 442 | u_out -= frame->linesize[1]; |
| 443 | v_out -= frame->linesize[2]; |
| 444 | } |
| 445 | break; |
| 446 | case IMGTYPE_YUV420: |
| 447 | u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1]; |
| 448 | v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2]; |
| 449 | for (row = 0; row < height - 1; row += 2) { |
| 450 | for (col = 0; col < width - 1; col += 2) { |
| 451 | memcpy(y_out + col, encoded, 2); |
| 452 | encoded += 2; |
| 453 | memcpy(y_out + col - frame->linesize[0], encoded, 2); |
| 454 | encoded += 2; |
| 455 | u_out[col >> 1] = *encoded++ + 128; |
| 456 | v_out[col >> 1] = *encoded++ + 128; |
| 457 | } |
| 458 | y_out -= frame->linesize[0] << 1; |
| 459 | u_out -= frame->linesize[1]; |
| 460 | v_out -= frame->linesize[2]; |
| 461 | } |
| 462 | break; |
| 463 | default: |
| 464 | av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n"); |
| 465 | return AVERROR_INVALIDDATA; |
| 466 | } |
| 467 | |
| 468 | *got_frame = 1; |
| 469 | |
| 470 | /* always report that the buffer was completely consumed */ |
| 471 | return buf_size; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * |
| 476 | * Init lcl decoder |
| 477 | * |
| 478 | */ |
| 479 | static av_cold int decode_init(AVCodecContext *avctx) |
| 480 | { |
| 481 | LclDecContext * const c = avctx->priv_data; |
| 482 | unsigned int basesize = avctx->width * avctx->height; |
| 483 | unsigned int max_basesize = FFALIGN(avctx->width, 4) * |
| 484 | FFALIGN(avctx->height, 4); |
| 485 | unsigned int max_decomp_size; |
| 486 | int subsample_h, subsample_v; |
| 487 | |
| 488 | if (avctx->extradata_size < 8) { |
| 489 | av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n"); |
| 490 | return AVERROR_INVALIDDATA; |
| 491 | } |
| 492 | |
| 493 | /* Check codec type */ |
| 494 | if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) || |
| 495 | (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) { |
| 496 | av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n"); |
| 497 | } |
| 498 | |
| 499 | /* Detect image type */ |
| 500 | switch (c->imgtype = avctx->extradata[4]) { |
| 501 | case IMGTYPE_YUV111: |
| 502 | c->decomp_size = basesize * 3; |
| 503 | max_decomp_size = max_basesize * 3; |
| 504 | avctx->pix_fmt = AV_PIX_FMT_YUV444P; |
| 505 | av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n"); |
| 506 | break; |
| 507 | case IMGTYPE_YUV422: |
| 508 | c->decomp_size = basesize * 2; |
| 509 | max_decomp_size = max_basesize * 2; |
| 510 | avctx->pix_fmt = AV_PIX_FMT_YUV422P; |
| 511 | av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n"); |
| 512 | if (avctx->width % 4) { |
| 513 | avpriv_request_sample(avctx, "Unsupported dimensions\n"); |
| 514 | return AVERROR_INVALIDDATA; |
| 515 | } |
| 516 | break; |
| 517 | case IMGTYPE_RGB24: |
| 518 | c->decomp_size = basesize * 3; |
| 519 | max_decomp_size = max_basesize * 3; |
| 520 | avctx->pix_fmt = AV_PIX_FMT_BGR24; |
| 521 | av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n"); |
| 522 | break; |
| 523 | case IMGTYPE_YUV411: |
| 524 | c->decomp_size = basesize / 2 * 3; |
| 525 | max_decomp_size = max_basesize / 2 * 3; |
| 526 | avctx->pix_fmt = AV_PIX_FMT_YUV411P; |
| 527 | av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n"); |
| 528 | break; |
| 529 | case IMGTYPE_YUV211: |
| 530 | c->decomp_size = basesize * 2; |
| 531 | max_decomp_size = max_basesize * 2; |
| 532 | avctx->pix_fmt = AV_PIX_FMT_YUV422P; |
| 533 | av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n"); |
| 534 | break; |
| 535 | case IMGTYPE_YUV420: |
| 536 | c->decomp_size = basesize / 2 * 3; |
| 537 | max_decomp_size = max_basesize / 2 * 3; |
| 538 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
| 539 | av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n"); |
| 540 | break; |
| 541 | default: |
| 542 | av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype); |
| 543 | return AVERROR_INVALIDDATA; |
| 544 | } |
| 545 | |
| 546 | av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v); |
| 547 | if (avctx->width % (1<<subsample_h) || avctx->height % (1<<subsample_v)) { |
| 548 | avpriv_request_sample(avctx, "Unsupported dimensions\n"); |
| 549 | return AVERROR_INVALIDDATA; |
| 550 | } |
| 551 | |
| 552 | /* Detect compression method */ |
| 553 | c->compression = (int8_t)avctx->extradata[5]; |
| 554 | switch (avctx->codec_id) { |
| 555 | case AV_CODEC_ID_MSZH: |
| 556 | switch (c->compression) { |
| 557 | case COMP_MSZH: |
| 558 | av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n"); |
| 559 | break; |
| 560 | case COMP_MSZH_NOCOMP: |
| 561 | c->decomp_size = 0; |
| 562 | av_log(avctx, AV_LOG_DEBUG, "No compression.\n"); |
| 563 | break; |
| 564 | default: |
| 565 | av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression); |
| 566 | return AVERROR_INVALIDDATA; |
| 567 | } |
| 568 | break; |
| 569 | #if CONFIG_ZLIB_DECODER |
| 570 | case AV_CODEC_ID_ZLIB: |
| 571 | switch (c->compression) { |
| 572 | case COMP_ZLIB_HISPEED: |
| 573 | av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n"); |
| 574 | break; |
| 575 | case COMP_ZLIB_HICOMP: |
| 576 | av_log(avctx, AV_LOG_DEBUG, "High compression.\n"); |
| 577 | break; |
| 578 | case COMP_ZLIB_NORMAL: |
| 579 | av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n"); |
| 580 | break; |
| 581 | default: |
| 582 | if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) { |
| 583 | av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression); |
| 584 | return AVERROR_INVALIDDATA; |
| 585 | } |
| 586 | av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression); |
| 587 | } |
| 588 | break; |
| 589 | #endif |
| 590 | default: |
| 591 | av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n"); |
| 592 | return AVERROR_INVALIDDATA; |
| 593 | } |
| 594 | |
| 595 | /* Allocate decompression buffer */ |
| 596 | if (c->decomp_size) { |
| 597 | if (!(c->decomp_buf = av_malloc(max_decomp_size))) { |
| 598 | av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); |
| 599 | return AVERROR(ENOMEM); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /* Detect flags */ |
| 604 | c->flags = avctx->extradata[6]; |
| 605 | if (c->flags & FLAG_MULTITHREAD) |
| 606 | av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n"); |
| 607 | if (c->flags & FLAG_NULLFRAME) |
| 608 | av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n"); |
| 609 | if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) |
| 610 | av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n"); |
| 611 | if (c->flags & FLAGMASK_UNUSED) |
| 612 | av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags); |
| 613 | |
| 614 | /* If needed init zlib */ |
| 615 | #if CONFIG_ZLIB_DECODER |
| 616 | if (avctx->codec_id == AV_CODEC_ID_ZLIB) { |
| 617 | int zret; |
| 618 | c->zstream.zalloc = Z_NULL; |
| 619 | c->zstream.zfree = Z_NULL; |
| 620 | c->zstream.opaque = Z_NULL; |
| 621 | zret = inflateInit(&c->zstream); |
| 622 | if (zret != Z_OK) { |
| 623 | av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); |
| 624 | av_freep(&c->decomp_buf); |
| 625 | return AVERROR_UNKNOWN; |
| 626 | } |
| 627 | } |
| 628 | #endif |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * |
| 635 | * Uninit lcl decoder |
| 636 | * |
| 637 | */ |
| 638 | static av_cold int decode_end(AVCodecContext *avctx) |
| 639 | { |
| 640 | LclDecContext * const c = avctx->priv_data; |
| 641 | |
| 642 | av_freep(&c->decomp_buf); |
| 643 | #if CONFIG_ZLIB_DECODER |
| 644 | if (avctx->codec_id == AV_CODEC_ID_ZLIB) |
| 645 | inflateEnd(&c->zstream); |
| 646 | #endif |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | #if CONFIG_MSZH_DECODER |
| 652 | AVCodec ff_mszh_decoder = { |
| 653 | .name = "mszh", |
| 654 | .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"), |
| 655 | .type = AVMEDIA_TYPE_VIDEO, |
| 656 | .id = AV_CODEC_ID_MSZH, |
| 657 | .priv_data_size = sizeof(LclDecContext), |
| 658 | .init = decode_init, |
| 659 | .close = decode_end, |
| 660 | .decode = decode_frame, |
| 661 | .capabilities = CODEC_CAP_DR1, |
| 662 | }; |
| 663 | #endif |
| 664 | |
| 665 | #if CONFIG_ZLIB_DECODER |
| 666 | AVCodec ff_zlib_decoder = { |
| 667 | .name = "zlib", |
| 668 | .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"), |
| 669 | .type = AVMEDIA_TYPE_VIDEO, |
| 670 | .id = AV_CODEC_ID_ZLIB, |
| 671 | .priv_data_size = sizeof(LclDecContext), |
| 672 | .init = decode_init, |
| 673 | .close = decode_end, |
| 674 | .decode = decode_frame, |
| 675 | .capabilities = CODEC_CAP_DR1, |
| 676 | }; |
| 677 | #endif |