51 #if CONFIG_ZLIB_DECODER
66 #if CONFIG_ZLIB_DECODER
76 static unsigned int mszh_decomp(
const unsigned char * srcptr,
int srclen,
unsigned char * destptr,
unsigned int destsize)
78 unsigned char *destptr_bak = destptr;
79 unsigned char *destptr_end = destptr + destsize;
80 const unsigned char *srcptr_end = srcptr + srclen;
81 unsigned mask = *srcptr++;
82 unsigned maskbit = 0x80;
84 while (srcptr < srcptr_end && destptr < destptr_end) {
85 if (!(
mask & maskbit)) {
86 memcpy(destptr, srcptr, 4);
90 unsigned ofs = bytestream_get_le16(&srcptr);
91 unsigned cnt = (ofs >> 11) + 1;
93 ofs =
FFMIN(ofs, destptr - destptr_bak);
95 cnt =
FFMIN(cnt, destptr_end - destptr);
101 memset(destptr, 0, cnt);
109 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32)
break;
110 memcpy(destptr, srcptr, 32);
119 return destptr - destptr_bak;
123 #if CONFIG_ZLIB_DECODER
134 int zret = inflateReset(&
c->zstream);
139 c->zstream.next_in =
src;
140 c->zstream.avail_in = src_len;
141 c->zstream.next_out =
c->decomp_buf +
offset;
142 c->zstream.avail_out =
c->decomp_size -
offset;
143 zret =
inflate(&
c->zstream, Z_FINISH);
144 if (zret != Z_OK && zret != Z_STREAM_END) {
148 if (expected != (
unsigned int)
c->zstream.total_out) {
150 expected,
c->zstream.total_out);
153 return c->zstream.total_out;
162 const uint8_t *buf = avpkt->
data;
163 int buf_size = avpkt->
size;
165 unsigned int pixel_ptr;
167 unsigned char *encoded = avpkt->
data, *outptr;
168 uint8_t *y_out, *u_out, *v_out;
171 unsigned int mszh_dlen;
172 unsigned char yq, y1q, uq, vq;
174 unsigned int mthread_inlen, mthread_outlen;
175 unsigned int len = buf_size;
181 outptr =
frame->data[0];
186 switch (
c->compression) {
193 if (
len < 8 ||
len - 8 < mthread_inlen) {
197 mthread_outlen =
AV_RL32(buf + 4);
198 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
199 mszh_dlen =
mszh_decomp(buf + 8, mthread_inlen,
c->decomp_buf,
c->decomp_size);
200 if (mthread_outlen != mszh_dlen) {
202 mthread_outlen, mszh_dlen);
205 mszh_dlen =
mszh_decomp(buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
206 c->decomp_buf + mthread_outlen,
c->decomp_size - mthread_outlen);
207 if (mthread_outlen != mszh_dlen) {
209 mthread_outlen, mszh_dlen);
212 encoded =
c->decomp_buf;
213 len =
c->decomp_size;
216 if (
c->decomp_size != mszh_dlen) {
218 c->decomp_size, mszh_dlen);
221 encoded =
c->decomp_buf;
227 switch (
c->imgtype) {
253 #if CONFIG_ZLIB_DECODER
261 memcpy(
c->decomp_buf, buf,
len);
262 encoded =
c->decomp_buf;
268 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
269 mthread_outlen =
AV_RL32(buf + 4);
270 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
271 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
273 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
274 mthread_outlen, mthread_outlen);
277 int ret = zlib_decomp(avctx, buf,
len, 0,
c->decomp_size);
280 encoded =
c->decomp_buf;
281 len =
c->decomp_size;
285 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
292 switch (
c->imgtype) {
295 for (row = 0; row <
height; row++) {
296 pixel_ptr = row *
width * 3;
297 yq = encoded[pixel_ptr++];
298 uqvq =
AV_RL16(encoded+pixel_ptr);
300 for (col = 1; col <
width; col++) {
301 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
302 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
303 AV_WL16(encoded+pixel_ptr+1, uqvq);
309 for (row = 0; row <
height; row++) {
310 pixel_ptr = row *
width * 2;
312 for (col = 0; col <
width/4; col++) {
313 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
314 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
315 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
316 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
317 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
318 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
319 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
320 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
326 for (row = 0; row <
height; row++) {
327 pixel_ptr = row *
width / 2 * 3;
329 for (col = 0; col <
width/4; col++) {
330 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
331 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
332 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
333 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
334 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
335 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
341 for (row = 0; row <
height; row++) {
342 pixel_ptr = row *
width * 2;
344 for (col = 0; col <
width/2; col++) {
345 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
346 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
347 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
348 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
354 for (row = 0; row <
height/2; row++) {
355 pixel_ptr = row *
width * 3;
356 yq = y1q = uq = vq =0;
357 for (col = 0; col <
width/2; col++) {
358 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
359 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
360 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
361 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
362 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
363 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
380 switch (
c->imgtype) {
382 for (row = 0; row <
height; row++) {
383 for (col = 0; col <
width; col++) {
384 y_out[col] = *encoded++;
385 u_out[col] = *encoded++ + 128;
386 v_out[col] = *encoded++ + 128;
388 y_out -=
frame->linesize[0];
389 u_out -=
frame->linesize[1];
390 v_out -=
frame->linesize[2];
394 for (row = 0; row <
height; row++) {
395 for (col = 0; col <
width - 3; col += 4) {
396 memcpy(y_out + col, encoded, 4);
398 u_out[ col >> 1 ] = *encoded++ + 128;
399 u_out[(col >> 1) + 1] = *encoded++ + 128;
400 v_out[ col >> 1 ] = *encoded++ + 128;
401 v_out[(col >> 1) + 1] = *encoded++ + 128;
403 y_out -=
frame->linesize[0];
404 u_out -=
frame->linesize[1];
405 v_out -=
frame->linesize[2];
410 for (row =
height - 1; row >= 0; row--) {
411 pixel_ptr = row *
frame->linesize[0];
412 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
417 for (row = 0; row <
height; row++) {
418 for (col = 0; col <
width - 3; col += 4) {
419 memcpy(y_out + col, encoded, 4);
421 u_out[col >> 2] = *encoded++ + 128;
422 v_out[col >> 2] = *encoded++ + 128;
424 y_out -=
frame->linesize[0];
425 u_out -=
frame->linesize[1];
426 v_out -=
frame->linesize[2];
430 for (row = 0; row <
height; row++) {
431 for (col = 0; col <
width - 1; col += 2) {
432 memcpy(y_out + col, encoded, 2);
434 u_out[col >> 1] = *encoded++ + 128;
435 v_out[col >> 1] = *encoded++ + 128;
437 y_out -=
frame->linesize[0];
438 u_out -=
frame->linesize[1];
439 v_out -=
frame->linesize[2];
445 for (row = 0; row <
height - 1; row += 2) {
446 for (col = 0; col <
width - 1; col += 2) {
447 memcpy(y_out + col, encoded, 2);
449 memcpy(y_out + col -
frame->linesize[0], encoded, 2);
451 u_out[col >> 1] = *encoded++ + 128;
452 v_out[col >> 1] = *encoded++ + 128;
454 y_out -=
frame->linesize[0] << 1;
455 u_out -=
frame->linesize[1];
456 v_out -=
frame->linesize[2];
464 frame->key_frame = 1;
476 unsigned int basesize = avctx->
width * avctx->
height;
479 unsigned int max_decomp_size;
480 int subsample_h, subsample_v;
490 av_log(avctx,
AV_LOG_ERROR,
"Codec id and codec type mismatch. This should not happen.\n");
496 c->decomp_size = basesize * 3;
497 max_decomp_size = max_basesize * 3;
502 c->decomp_size = basesize * 2;
503 max_decomp_size = max_basesize * 2;
506 if (avctx->
width % 4) {
512 c->decomp_size = basesize * 3;
513 max_decomp_size = max_basesize * 3;
518 c->decomp_size = basesize / 2 * 3;
519 max_decomp_size = max_basesize / 2 * 3;
524 c->decomp_size = basesize * 2;
525 max_decomp_size = max_basesize * 2;
530 c->decomp_size = basesize / 2 * 3;
531 max_decomp_size = max_basesize / 2 * 3;
541 if (avctx->
width % (1<<subsample_h) || avctx->
height % (1<<subsample_v)) {
550 switch (
c->compression) {
559 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression format for MSZH (%d).\n",
c->compression);
563 #if CONFIG_ZLIB_DECODER
565 switch (
c->compression) {
576 if (
c->compression < Z_NO_COMPRESSION ||
c->compression > Z_BEST_COMPRESSION) {
577 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression level for ZLIB: (%d).\n",
c->compression);
590 if (
c->decomp_size) {
591 if (!(
c->decomp_buf =
av_malloc(max_decomp_size))) {
609 #if CONFIG_ZLIB_DECODER
612 c->zstream.zalloc = Z_NULL;
613 c->zstream.zfree = Z_NULL;
614 c->zstream.opaque = Z_NULL;
615 zret = inflateInit(&
c->zstream);
632 #if CONFIG_ZLIB_DECODER
634 inflateEnd(&
c->zstream);
640 #if CONFIG_MSZH_DECODER
655 #if CONFIG_ZLIB_DECODER