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;
186 outptr =
frame->data[0];
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 switch (
c->imgtype) {
260 #if CONFIG_ZLIB_DECODER
268 memcpy(
c->decomp_buf, buf,
len);
269 encoded =
c->decomp_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);
280 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
281 mthread_outlen, mthread_outlen);
283 len =
c->decomp_size;
285 int ret = zlib_decomp(avctx, buf,
len, 0,
c->decomp_size);
289 encoded =
c->decomp_buf;
293 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
300 switch (
c->imgtype) {
303 for (row = 0; row <
height; row++) {
304 pixel_ptr = row *
width * 3;
305 yq = encoded[pixel_ptr++];
306 uqvq =
AV_RL16(encoded+pixel_ptr);
308 for (col = 1; col <
width; col++) {
309 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
310 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
311 AV_WL16(encoded+pixel_ptr+1, uqvq);
317 for (row = 0; row <
height; row++) {
318 pixel_ptr = row *
width * 2;
320 for (col = 0; col <
width/4; col++) {
321 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
322 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
323 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
324 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
325 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
326 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
327 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
328 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
334 for (row = 0; row <
height; row++) {
335 pixel_ptr = row *
width / 2 * 3;
337 for (col = 0; col <
width/4; col++) {
338 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
339 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
340 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
341 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
342 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
343 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
349 for (row = 0; row <
height; row++) {
350 pixel_ptr = row *
width * 2;
352 for (col = 0; col <
width/2; col++) {
353 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
354 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
355 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
356 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
362 for (row = 0; row <
height/2; row++) {
363 pixel_ptr = row *
width * 3;
364 yq = y1q = uq = vq =0;
365 for (col = 0; col <
width/2; col++) {
366 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
367 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
368 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
369 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
370 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
371 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
388 switch (
c->imgtype) {
390 for (row = 0; row <
height; row++) {
391 for (col = 0; col <
width; col++) {
392 y_out[col] = *encoded++;
393 u_out[col] = *encoded++ + 128;
394 v_out[col] = *encoded++ + 128;
396 y_out -=
frame->linesize[0];
397 u_out -=
frame->linesize[1];
398 v_out -=
frame->linesize[2];
402 for (row = 0; row <
height; row++) {
403 for (col = 0; col <
width - 3; col += 4) {
404 memcpy(y_out + col, encoded, 4);
406 u_out[ col >> 1 ] = *encoded++ + 128;
407 u_out[(col >> 1) + 1] = *encoded++ + 128;
408 v_out[ col >> 1 ] = *encoded++ + 128;
409 v_out[(col >> 1) + 1] = *encoded++ + 128;
411 if (col && col <
width) {
412 u_out[ col >> 1 ] = u_out[(col>>1) - 1];
413 v_out[ col >> 1 ] = v_out[(col>>1) - 1];
416 y_out -=
frame->linesize[0];
417 u_out -=
frame->linesize[1];
418 v_out -=
frame->linesize[2];
423 for (row =
height - 1; row >= 0; row--) {
424 pixel_ptr = row *
frame->linesize[0];
425 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
430 for (row = 0; row <
height; row++) {
431 for (col = 0; col <
width - 3; col += 4) {
432 memcpy(y_out + col, encoded, 4);
434 u_out[col >> 2] = *encoded++ + 128;
435 v_out[col >> 2] = *encoded++ + 128;
437 if (col && col <
width) {
438 u_out[col >> 2] = u_out[(col>>2) - 1];
439 v_out[col >> 2] = v_out[(col>>2) - 1];
441 y_out -=
frame->linesize[0];
442 u_out -=
frame->linesize[1];
443 v_out -=
frame->linesize[2];
447 for (row = 0; row <
height; row++) {
448 for (col = 0; col <
width - 1; col += 2) {
449 memcpy(y_out + col, encoded, 2);
451 u_out[col >> 1] = *encoded++ + 128;
452 v_out[col >> 1] = *encoded++ + 128;
454 y_out -=
frame->linesize[0];
455 u_out -=
frame->linesize[1];
456 v_out -=
frame->linesize[2];
462 for (row = 0; row <
height - 1; row += 2) {
463 for (col = 0; col <
width - 1; col += 2) {
464 memcpy(y_out + col, encoded, 2);
466 memcpy(y_out + col -
frame->linesize[0], encoded, 2);
468 u_out[col >> 1] = *encoded++ + 128;
469 v_out[col >> 1] = *encoded++ + 128;
471 y_out -=
frame->linesize[0] << 1;
472 u_out -=
frame->linesize[1];
473 v_out -=
frame->linesize[2];
493 unsigned int basesize = avctx->
width * avctx->
height;
496 unsigned int max_decomp_size;
497 int subsample_h, subsample_v;
498 int partial_h_supported = 0;
508 av_log(avctx,
AV_LOG_ERROR,
"Codec id and codec type mismatch. This should not happen.\n");
514 c->decomp_size = basesize * 3;
515 max_decomp_size = max_basesize * 3;
520 c->decomp_size = (avctx->
width & ~3) * avctx->
height * 2;
521 max_decomp_size = max_basesize * 2;
524 partial_h_supported = 1;
528 max_decomp_size = max_basesize * 3;
533 c->decomp_size = (avctx->
width & ~3) * avctx->
height / 2 * 3;
534 max_decomp_size = max_basesize / 2 * 3;
537 partial_h_supported = 1;
540 c->decomp_size = basesize * 2;
541 max_decomp_size = max_basesize * 2;
546 c->decomp_size = basesize / 2 * 3;
547 max_decomp_size = max_basesize / 2 * 3;
557 if ((avctx->
width % (1<<subsample_h) && !partial_h_supported) || avctx->
height % (1<<subsample_v)) {
566 switch (
c->compression) {
575 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression format for MSZH (%d).\n",
c->compression);
579 #if CONFIG_ZLIB_DECODER
581 switch (
c->compression) {
592 if (
c->compression < Z_NO_COMPRESSION ||
c->compression > Z_BEST_COMPRESSION) {
593 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression level for ZLIB: (%d).\n",
c->compression);
606 if (
c->decomp_size) {
607 if (!(
c->decomp_buf =
av_malloc(max_decomp_size))) {
625 #if CONFIG_ZLIB_DECODER
638 #if CONFIG_ZLIB_DECODER
645 #if CONFIG_MSZH_DECODER
660 #if CONFIG_ZLIB_DECODER