40 #include "config_components.h"
53 #if CONFIG_ZLIB_DECODER
69 #if CONFIG_ZLIB_DECODER
79 static unsigned int mszh_decomp(
const unsigned char * srcptr,
int srclen,
unsigned char * destptr,
unsigned int destsize)
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;
87 while (srcptr < srcptr_end && destptr < destptr_end) {
88 if (!(
mask & maskbit)) {
89 memcpy(destptr, srcptr, 4);
93 unsigned ofs = bytestream_get_le16(&srcptr);
94 unsigned cnt = (ofs >> 11) + 1;
96 ofs =
FFMIN(ofs, destptr - destptr_bak);
98 cnt =
FFMIN(cnt, destptr_end - destptr);
104 memset(destptr, 0, cnt);
112 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32)
break;
113 memcpy(destptr, srcptr, 32);
122 return destptr - destptr_bak;
126 #if CONFIG_ZLIB_DECODER
137 z_stream *
const zstream = &
c->zstream.zstream;
138 int zret = inflateReset(zstream);
143 zstream->next_in =
src;
144 zstream->avail_in = src_len;
145 zstream->next_out =
c->decomp_buf +
offset;
146 zstream->avail_out =
c->decomp_size -
offset;
147 zret =
inflate(zstream, Z_FINISH);
148 if (zret != Z_OK && zret != Z_STREAM_END) {
152 if (expected != (
unsigned int)zstream->total_out) {
154 expected, zstream->total_out);
157 return zstream->total_out;
165 const uint8_t *buf = avpkt->
data;
166 int buf_size = avpkt->
size;
168 unsigned int pixel_ptr;
170 unsigned char *encoded = avpkt->
data, *outptr;
171 uint8_t *y_out, *u_out, *v_out;
174 unsigned int mszh_dlen;
175 unsigned char yq, y1q, uq, vq;
177 unsigned int mthread_inlen, mthread_outlen;
178 unsigned int len = buf_size;
184 outptr =
frame->data[0];
189 switch (
c->compression) {
196 if (
len < 8 ||
len - 8 < mthread_inlen) {
200 mthread_outlen =
AV_RL32(buf + 4);
201 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
202 mszh_dlen =
mszh_decomp(buf + 8, mthread_inlen,
c->decomp_buf,
c->decomp_size);
203 if (mthread_outlen != mszh_dlen) {
205 mthread_outlen, mszh_dlen);
208 mszh_dlen =
mszh_decomp(buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
209 c->decomp_buf + mthread_outlen,
c->decomp_size - mthread_outlen);
210 if (mthread_outlen != mszh_dlen) {
212 mthread_outlen, mszh_dlen);
215 encoded =
c->decomp_buf;
216 len =
c->decomp_size;
219 if (
c->decomp_size != mszh_dlen) {
221 c->decomp_size, mszh_dlen);
224 encoded =
c->decomp_buf;
230 switch (
c->imgtype) {
256 #if CONFIG_ZLIB_DECODER
264 memcpy(
c->decomp_buf, buf,
len);
265 encoded =
c->decomp_buf;
271 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
272 mthread_outlen =
AV_RL32(buf + 4);
273 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
274 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
276 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
277 mthread_outlen, mthread_outlen);
280 int ret = zlib_decomp(avctx, buf,
len, 0,
c->decomp_size);
283 encoded =
c->decomp_buf;
284 len =
c->decomp_size;
288 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
295 switch (
c->imgtype) {
298 for (row = 0; row <
height; row++) {
299 pixel_ptr = row *
width * 3;
300 yq = encoded[pixel_ptr++];
301 uqvq =
AV_RL16(encoded+pixel_ptr);
303 for (col = 1; col <
width; col++) {
304 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
305 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
306 AV_WL16(encoded+pixel_ptr+1, uqvq);
312 for (row = 0; row <
height; row++) {
313 pixel_ptr = row *
width * 2;
315 for (col = 0; col <
width/4; col++) {
316 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
317 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
318 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
319 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
320 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
321 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
322 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
323 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
329 for (row = 0; row <
height; row++) {
330 pixel_ptr = row *
width / 2 * 3;
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] = vq -= encoded[pixel_ptr+5];
344 for (row = 0; row <
height; row++) {
345 pixel_ptr = row *
width * 2;
347 for (col = 0; col <
width/2; col++) {
348 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
349 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
350 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
351 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
357 for (row = 0; row <
height/2; row++) {
358 pixel_ptr = row *
width * 3;
359 yq = y1q = uq = vq =0;
360 for (col = 0; col <
width/2; col++) {
361 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
362 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
363 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
364 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
365 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
366 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
383 switch (
c->imgtype) {
385 for (row = 0; row <
height; row++) {
386 for (col = 0; col <
width; col++) {
387 y_out[col] = *encoded++;
388 u_out[col] = *encoded++ + 128;
389 v_out[col] = *encoded++ + 128;
391 y_out -=
frame->linesize[0];
392 u_out -=
frame->linesize[1];
393 v_out -=
frame->linesize[2];
397 for (row = 0; row <
height; row++) {
398 for (col = 0; col <
width - 3; col += 4) {
399 memcpy(y_out + col, encoded, 4);
401 u_out[ col >> 1 ] = *encoded++ + 128;
402 u_out[(col >> 1) + 1] = *encoded++ + 128;
403 v_out[ col >> 1 ] = *encoded++ + 128;
404 v_out[(col >> 1) + 1] = *encoded++ + 128;
406 y_out -=
frame->linesize[0];
407 u_out -=
frame->linesize[1];
408 v_out -=
frame->linesize[2];
413 for (row =
height - 1; row >= 0; row--) {
414 pixel_ptr = row *
frame->linesize[0];
415 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
420 for (row = 0; row <
height; row++) {
421 for (col = 0; col <
width - 3; col += 4) {
422 memcpy(y_out + col, encoded, 4);
424 u_out[col >> 2] = *encoded++ + 128;
425 v_out[col >> 2] = *encoded++ + 128;
427 y_out -=
frame->linesize[0];
428 u_out -=
frame->linesize[1];
429 v_out -=
frame->linesize[2];
433 for (row = 0; row <
height; row++) {
434 for (col = 0; col <
width - 1; col += 2) {
435 memcpy(y_out + col, encoded, 2);
437 u_out[col >> 1] = *encoded++ + 128;
438 v_out[col >> 1] = *encoded++ + 128;
440 y_out -=
frame->linesize[0];
441 u_out -=
frame->linesize[1];
442 v_out -=
frame->linesize[2];
448 for (row = 0; row <
height - 1; row += 2) {
449 for (col = 0; col <
width - 1; col += 2) {
450 memcpy(y_out + col, encoded, 2);
452 memcpy(y_out + col -
frame->linesize[0], encoded, 2);
454 u_out[col >> 1] = *encoded++ + 128;
455 v_out[col >> 1] = *encoded++ + 128;
457 y_out -=
frame->linesize[0] << 1;
458 u_out -=
frame->linesize[1];
459 v_out -=
frame->linesize[2];
467 frame->key_frame = 1;
479 unsigned int basesize = avctx->
width * avctx->
height;
482 unsigned int max_decomp_size;
483 int subsample_h, subsample_v;
493 av_log(avctx,
AV_LOG_ERROR,
"Codec id and codec type mismatch. This should not happen.\n");
499 c->decomp_size = basesize * 3;
500 max_decomp_size = max_basesize * 3;
505 c->decomp_size = basesize * 2;
506 max_decomp_size = max_basesize * 2;
509 if (avctx->
width % 4) {
515 c->decomp_size = basesize * 3;
516 max_decomp_size = max_basesize * 3;
521 c->decomp_size = basesize / 2 * 3;
522 max_decomp_size = max_basesize / 2 * 3;
527 c->decomp_size = basesize * 2;
528 max_decomp_size = max_basesize * 2;
533 c->decomp_size = basesize / 2 * 3;
534 max_decomp_size = max_basesize / 2 * 3;
544 if (avctx->
width % (1<<subsample_h) || avctx->
height % (1<<subsample_v)) {
553 switch (
c->compression) {
562 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression format for MSZH (%d).\n",
c->compression);
566 #if CONFIG_ZLIB_DECODER
568 switch (
c->compression) {
579 if (
c->compression < Z_NO_COMPRESSION ||
c->compression > Z_BEST_COMPRESSION) {
580 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression level for ZLIB: (%d).\n",
c->compression);
593 if (
c->decomp_size) {
594 if (!(
c->decomp_buf =
av_malloc(max_decomp_size))) {
612 #if CONFIG_ZLIB_DECODER
625 #if CONFIG_ZLIB_DECODER
632 #if CONFIG_MSZH_DECODER
647 #if CONFIG_ZLIB_DECODER