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);
155 if (expected > (
unsigned int)zstream->total_out)
156 return (
unsigned int)zstream->total_out;
159 return zstream->total_out;
167 const uint8_t *buf = avpkt->
data;
168 int buf_size = avpkt->
size;
172 unsigned char *encoded = avpkt->
data, *outptr;
173 uint8_t *y_out, *u_out, *v_out;
176 unsigned int mszh_dlen;
177 unsigned char yq, y1q, uq, vq;
179 unsigned int mthread_inlen, mthread_outlen;
180 unsigned int len = buf_size;
191 switch (
c->compression) {
198 if (
len < 8 ||
len - 8 < mthread_inlen) {
202 mthread_outlen =
AV_RL32(buf + 4);
203 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
204 mszh_dlen =
mszh_decomp(buf + 8, mthread_inlen,
c->decomp_buf,
c->decomp_size);
205 if (mthread_outlen != mszh_dlen) {
207 mthread_outlen, mszh_dlen);
210 mszh_dlen =
mszh_decomp(buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
211 c->decomp_buf + mthread_outlen,
c->decomp_size - mthread_outlen);
212 if (mthread_outlen != mszh_dlen) {
214 mthread_outlen, mszh_dlen);
217 encoded =
c->decomp_buf;
218 len =
c->decomp_size;
221 if (
c->decomp_size != mszh_dlen) {
223 c->decomp_size, mszh_dlen);
224 if (
c->decomp_size != mszh_dlen &&
225 c->decomp_size != mszh_dlen + 2)
228 encoded =
c->decomp_buf;
234 int aligned_width =
width;
235 switch (
c->imgtype) {
254 if (
len < ((aligned_width *
height * bppx2) >> 1))
263 #if CONFIG_ZLIB_DECODER
271 memcpy(
c->decomp_buf, buf,
len);
272 encoded =
c->decomp_buf;
278 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
279 mthread_outlen =
AV_RL32(buf + 4);
280 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
281 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
283 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
284 mthread_outlen, mthread_outlen);
286 len =
c->decomp_size;
288 int ret = zlib_decomp(avctx, buf,
len, 0,
c->decomp_size);
292 encoded =
c->decomp_buf;
296 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
303 switch (
c->imgtype) {
306 for (row = 0; row <
height; row++) {
307 pixel_ptr = row *
width * 3;
308 yq = encoded[pixel_ptr++];
309 uqvq =
AV_RL16(encoded+pixel_ptr);
311 for (col = 1; col <
width; col++) {
312 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
313 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
314 AV_WL16(encoded+pixel_ptr+1, uqvq);
321 for (row = 0; row <
height; row++) {
323 for (col = 0; col <
width/4; col++) {
324 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
325 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
326 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
327 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
328 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
329 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
330 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
331 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
338 for (row = 0; row <
height; row++) {
340 for (col = 0; col <
width/4; col++) {
341 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
342 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
343 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
344 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
345 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
346 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
352 for (row = 0; row <
height; row++) {
353 pixel_ptr = row *
width * 2;
355 for (col = 0; col <
width/2; col++) {
356 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
357 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
358 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
359 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
365 for (row = 0; row <
height/2; row++) {
366 pixel_ptr = row *
width * 3;
367 yq = y1q = uq = vq =0;
368 for (col = 0; col <
width/2; col++) {
369 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
370 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
371 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
372 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
373 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
374 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
391 switch (
c->imgtype) {
393 for (row = 0; row <
height; row++) {
394 for (col = 0; col <
width; col++) {
395 y_out[col] = *encoded++;
396 u_out[col] = *encoded++ + 128;
397 v_out[col] = *encoded++ + 128;
405 for (row = 0; row <
height; row++) {
406 for (col = 0; col <
width - 3; col += 4) {
407 memcpy(y_out + col, encoded, 4);
409 u_out[ col >> 1 ] = *encoded++ + 128;
410 u_out[(col >> 1) + 1] = *encoded++ + 128;
411 v_out[ col >> 1 ] = *encoded++ + 128;
412 v_out[(col >> 1) + 1] = *encoded++ + 128;
414 if (col && col <
width) {
415 u_out[ col >> 1 ] = u_out[(col>>1) - 1];
416 v_out[ col >> 1 ] = v_out[(col>>1) - 1];
426 for (row =
height - 1; row >= 0; row--) {
428 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
433 for (row = 0; row <
height; row++) {
434 for (col = 0; col <
width - 3; col += 4) {
435 memcpy(y_out + col, encoded, 4);
437 u_out[col >> 2] = *encoded++ + 128;
438 v_out[col >> 2] = *encoded++ + 128;
440 if (col && col <
width) {
441 u_out[col >> 2] = u_out[(col>>2) - 1];
442 v_out[col >> 2] = v_out[(col>>2) - 1];
450 for (row = 0; row <
height; row++) {
451 for (col = 0; col <
width - 1; col += 2) {
452 memcpy(y_out + col, encoded, 2);
454 u_out[col >> 1] = *encoded++ + 128;
455 v_out[col >> 1] = *encoded++ + 128;
465 for (row = 0; row <
height - 1; row += 2) {
466 for (col = 0; col <
width - 1; col += 2) {
467 memcpy(y_out + col, encoded, 2);
471 u_out[col >> 1] = *encoded++ + 128;
472 v_out[col >> 1] = *encoded++ + 128;
496 unsigned int basesize = avctx->
width * avctx->
height;
499 unsigned int max_decomp_size;
500 int subsample_h, subsample_v;
501 int partial_h_supported = 0;
511 av_log(avctx,
AV_LOG_ERROR,
"Codec id and codec type mismatch. This should not happen.\n");
517 c->decomp_size = basesize * 3;
518 max_decomp_size = max_basesize * 3;
523 c->decomp_size = (avctx->
width & ~3) * avctx->
height * 2;
524 max_decomp_size = max_basesize * 2;
527 partial_h_supported = 1;
531 max_decomp_size = max_basesize * 3;
536 c->decomp_size = (avctx->
width & ~3) * avctx->
height / 2 * 3;
537 max_decomp_size = max_basesize / 2 * 3;
540 partial_h_supported = 1;
543 c->decomp_size = basesize * 2;
544 max_decomp_size = max_basesize * 2;
549 c->decomp_size = basesize / 2 * 3;
550 max_decomp_size = max_basesize / 2 * 3;
560 if ((avctx->
width % (1<<subsample_h) && !partial_h_supported) || avctx->
height % (1<<subsample_v)) {
569 switch (
c->compression) {
578 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression format for MSZH (%d).\n",
c->compression);
582 #if CONFIG_ZLIB_DECODER
584 switch (
c->compression) {
595 if (
c->compression < Z_NO_COMPRESSION ||
c->compression > Z_BEST_COMPRESSION) {
596 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression level for ZLIB: (%d).\n",
c->compression);
609 if (
c->decomp_size) {
610 if (!(
c->decomp_buf =
av_malloc(max_decomp_size))) {
628 #if CONFIG_ZLIB_DECODER
641 #if CONFIG_ZLIB_DECODER
648 #if CONFIG_MSZH_DECODER
663 #if CONFIG_ZLIB_DECODER