40 #include "config_components.h"
54 #if CONFIG_ZLIB_DECODER
70 #if CONFIG_ZLIB_DECODER
80 static unsigned int mszh_decomp(
const unsigned char * srcptr,
int srclen,
unsigned char * destptr,
unsigned int destsize)
82 unsigned char *destptr_bak = destptr;
83 unsigned char *destptr_end = destptr + destsize;
84 const unsigned char *srcptr_end = srcptr + srclen;
85 unsigned mask = *srcptr++;
86 unsigned maskbit = 0x80;
88 while (srcptr < srcptr_end && destptr < destptr_end) {
89 if (!(
mask & maskbit)) {
90 memcpy(destptr, srcptr, 4);
94 unsigned ofs = bytestream_get_le16(&srcptr);
95 unsigned cnt = (ofs >> 11) + 1;
97 ofs =
FFMIN(ofs, destptr - destptr_bak);
99 cnt =
FFMIN(cnt, destptr_end - destptr);
105 memset(destptr, 0, cnt);
113 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32)
break;
114 memcpy(destptr, srcptr, 32);
123 if (destptr < destptr_end)
124 memset(destptr, 0, destptr_end - destptr);
126 return destptr - destptr_bak;
130 #if CONFIG_ZLIB_DECODER
141 z_stream *
const zstream = &
c->zstream.zstream;
142 int zret = inflateReset(zstream);
147 zstream->next_in =
src;
148 zstream->avail_in = src_len;
149 zstream->next_out =
c->decomp_buf +
offset;
150 zstream->avail_out =
c->decomp_size -
offset;
151 zret =
inflate(zstream, Z_FINISH);
152 if (zret != Z_OK && zret != Z_STREAM_END) {
156 if (expected != (
unsigned int)zstream->total_out) {
158 expected, zstream->total_out);
159 if (expected > (
unsigned int)zstream->total_out) {
160 memset(
c->decomp_buf +
offset + zstream->total_out, 0,
161 c->decomp_size -
offset - zstream->total_out);
162 return (
unsigned int)zstream->total_out;
166 return zstream->total_out;
174 const uint8_t *buf = avpkt->
data;
175 int buf_size = avpkt->
size;
179 unsigned char *encoded = avpkt->
data, *outptr;
180 uint8_t *y_out, *u_out, *v_out;
183 unsigned int mszh_dlen;
184 unsigned char yq, y1q, uq, vq;
186 unsigned int mthread_inlen, mthread_outlen;
187 unsigned int len = buf_size;
193 outptr =
frame->data[0];
198 switch (
c->compression) {
205 if (
len < 8 ||
len - 8 < mthread_inlen) {
209 mthread_outlen =
AV_RL32(buf + 4);
210 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
211 mszh_dlen =
mszh_decomp(buf + 8, mthread_inlen,
c->decomp_buf,
c->decomp_size);
212 if (mthread_outlen != mszh_dlen) {
214 mthread_outlen, mszh_dlen);
217 mszh_dlen =
mszh_decomp(buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
218 c->decomp_buf + mthread_outlen,
c->decomp_size - mthread_outlen);
219 if (mthread_outlen != mszh_dlen) {
221 mthread_outlen, mszh_dlen);
224 encoded =
c->decomp_buf;
225 len =
c->decomp_size;
228 if (
c->decomp_size != mszh_dlen) {
230 c->decomp_size, mszh_dlen);
231 if (
c->decomp_size != mszh_dlen &&
232 c->decomp_size != mszh_dlen + 2)
235 encoded =
c->decomp_buf;
241 int aligned_width =
width;
242 switch (
c->imgtype) {
263 if (
len < ((aligned_width *
height * bppx2) >> 1))
272 #if CONFIG_ZLIB_DECODER
280 memcpy(
c->decomp_buf, buf,
len);
281 encoded =
c->decomp_buf;
287 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
288 mthread_outlen =
AV_RL32(buf + 4);
289 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
290 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
292 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
293 mthread_outlen, mthread_outlen);
295 len =
c->decomp_size;
297 int ret = zlib_decomp(avctx, buf,
len, 0,
c->decomp_size);
301 encoded =
c->decomp_buf;
305 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
312 switch (
c->imgtype) {
315 for (row = 0; row <
height; row++) {
316 pixel_ptr = row *
width * 3;
317 yq = encoded[pixel_ptr++];
318 unsigned uqvq =
AV_RL16(encoded+pixel_ptr);
320 for (col = 1; col <
width; col++) {
321 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
322 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
323 AV_WL16(encoded+pixel_ptr+1, uqvq);
330 for (row = 0; row <
height; row++) {
332 for (col = 0; col <
width/4; col++) {
333 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
334 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
335 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
336 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
337 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
338 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
339 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
340 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
347 for (row = 0; row <
height; row++) {
349 for (col = 0; col <
width/4; col++) {
350 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
351 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
352 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
353 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
354 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
355 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
361 for (row = 0; row <
height; row++) {
362 pixel_ptr = row *
width * 2;
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] = uq -= encoded[pixel_ptr+2];
368 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
374 for (row = 0; row <
height/2; row++) {
375 pixel_ptr = row *
width * 3;
376 yq = y1q = uq = vq =0;
377 for (col = 0; col <
width/2; col++) {
378 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
379 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
380 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
381 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
382 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
383 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
400 switch (
c->imgtype) {
402 for (row = 0; row <
height; row++) {
403 for (col = 0; col <
width; col++) {
404 y_out[col] = *encoded++;
405 u_out[col] = *encoded++ + 128;
406 v_out[col] = *encoded++ + 128;
408 y_out -=
frame->linesize[0];
409 u_out -=
frame->linesize[1];
410 v_out -=
frame->linesize[2];
414 for (row = 0; row <
height; row++) {
415 for (col = 0; col <
width - 3; col += 4) {
416 memcpy(y_out + col, encoded, 4);
418 u_out[ col >> 1 ] = *encoded++ + 128;
419 u_out[(col >> 1) + 1] = *encoded++ + 128;
420 v_out[ col >> 1 ] = *encoded++ + 128;
421 v_out[(col >> 1) + 1] = *encoded++ + 128;
423 if (col && col <
width) {
424 u_out[ col >> 1 ] = u_out[(col>>1) - 1];
425 v_out[ col >> 1 ] = v_out[(col>>1) - 1];
428 y_out -=
frame->linesize[0];
429 u_out -=
frame->linesize[1];
430 v_out -=
frame->linesize[2];
435 for (row =
height - 1; row >= 0; row--) {
436 pixel_ptr = row *
frame->linesize[0];
437 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
442 for (row = 0; row <
height; row++) {
443 for (col = 0; col <
width - 3; col += 4) {
444 memcpy(y_out + col, encoded, 4);
446 u_out[col >> 2] = *encoded++ + 128;
447 v_out[col >> 2] = *encoded++ + 128;
449 if (col && col <
width) {
450 u_out[col >> 2] = u_out[(col>>2) - 1];
451 v_out[col >> 2] = v_out[(col>>2) - 1];
453 y_out -=
frame->linesize[0];
454 u_out -=
frame->linesize[1];
455 v_out -=
frame->linesize[2];
459 for (row = 0; row <
height; row++) {
460 for (col = 0; col <
width - 1; col += 2) {
461 memcpy(y_out + col, encoded, 2);
463 u_out[col >> 1] = *encoded++ + 128;
464 v_out[col >> 1] = *encoded++ + 128;
466 y_out -=
frame->linesize[0];
467 u_out -=
frame->linesize[1];
468 v_out -=
frame->linesize[2];
474 for (row = 0; row <
height - 1; row += 2) {
475 for (col = 0; col <
width - 1; col += 2) {
476 memcpy(y_out + col, encoded, 2);
478 memcpy(y_out + col -
frame->linesize[0], encoded, 2);
480 u_out[col >> 1] = *encoded++ + 128;
481 v_out[col >> 1] = *encoded++ + 128;
483 y_out -=
frame->linesize[0] << 1;
484 u_out -=
frame->linesize[1];
485 v_out -=
frame->linesize[2];
502 unsigned int basesize = avctx->
width * avctx->
height;
505 unsigned int max_decomp_size;
506 int subsample_h, subsample_v;
507 int partial_h_supported = 0;
517 av_log(avctx,
AV_LOG_ERROR,
"Codec id and codec type mismatch. This should not happen.\n");
523 c->decomp_size = basesize * 3;
524 max_decomp_size = max_basesize * 3;
529 c->decomp_size = (avctx->
width & ~3) * avctx->
height * 2;
530 max_decomp_size = max_basesize * 2;
533 partial_h_supported = 1;
537 max_decomp_size = max_basesize * 3;
542 c->decomp_size = (avctx->
width & ~3) * avctx->
height / 2 * 3;
543 max_decomp_size = max_basesize / 2 * 3;
546 partial_h_supported = 1;
549 c->decomp_size = basesize * 2;
550 max_decomp_size = max_basesize * 2;
555 c->decomp_size = basesize / 2 * 3;
556 max_decomp_size = max_basesize / 2 * 3;
566 if ((avctx->
width % (1<<subsample_h) && !partial_h_supported) || avctx->
height % (1<<subsample_v)) {
575 switch (
c->compression) {
584 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression format for MSZH (%d).\n",
c->compression);
588 #if CONFIG_ZLIB_DECODER
590 switch (
c->compression) {
601 if (
c->compression < Z_NO_COMPRESSION ||
c->compression > Z_BEST_COMPRESSION) {
602 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression level for ZLIB: (%d).\n",
c->compression);
615 if (
c->decomp_size) {
616 if (!(
c->decomp_buf =
av_malloc(max_decomp_size))) {
634 #if CONFIG_ZLIB_DECODER
647 #if CONFIG_ZLIB_DECODER
654 #if CONFIG_MSZH_DECODER
669 #if CONFIG_ZLIB_DECODER