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 return destptr - destptr_bak;
127 #if CONFIG_ZLIB_DECODER
138 z_stream *
const zstream = &
c->zstream.zstream;
139 int zret = inflateReset(zstream);
144 zstream->next_in =
src;
145 zstream->avail_in = src_len;
146 zstream->next_out =
c->decomp_buf +
offset;
147 zstream->avail_out =
c->decomp_size -
offset;
148 zret =
inflate(zstream, Z_FINISH);
149 if (zret != Z_OK && zret != Z_STREAM_END) {
153 if (expected != (
unsigned int)zstream->total_out) {
155 expected, zstream->total_out);
156 if (expected > (
unsigned int)zstream->total_out)
157 return (
unsigned int)zstream->total_out;
160 return zstream->total_out;
168 const uint8_t *buf = avpkt->
data;
169 int buf_size = avpkt->
size;
173 unsigned char *encoded = avpkt->
data, *outptr;
174 uint8_t *y_out, *u_out, *v_out;
177 unsigned int mszh_dlen;
178 unsigned char yq, y1q, uq, vq;
180 unsigned int mthread_inlen, mthread_outlen;
181 unsigned int len = buf_size;
187 outptr =
frame->data[0];
192 switch (
c->compression) {
199 if (
len < 8 ||
len - 8 < mthread_inlen) {
203 mthread_outlen =
AV_RL32(buf + 4);
204 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
205 mszh_dlen =
mszh_decomp(buf + 8, mthread_inlen,
c->decomp_buf,
c->decomp_size);
206 if (mthread_outlen != mszh_dlen) {
208 mthread_outlen, mszh_dlen);
211 mszh_dlen =
mszh_decomp(buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
212 c->decomp_buf + mthread_outlen,
c->decomp_size - mthread_outlen);
213 if (mthread_outlen != mszh_dlen) {
215 mthread_outlen, mszh_dlen);
218 encoded =
c->decomp_buf;
219 len =
c->decomp_size;
222 if (
c->decomp_size != mszh_dlen) {
224 c->decomp_size, mszh_dlen);
225 if (
c->decomp_size != mszh_dlen &&
226 c->decomp_size != mszh_dlen + 2)
229 encoded =
c->decomp_buf;
235 int aligned_width =
width;
236 switch (
c->imgtype) {
257 if (
len < ((aligned_width *
height * bppx2) >> 1))
266 #if CONFIG_ZLIB_DECODER
274 memcpy(
c->decomp_buf, buf,
len);
275 encoded =
c->decomp_buf;
281 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
282 mthread_outlen =
AV_RL32(buf + 4);
283 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
284 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
286 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
287 mthread_outlen, mthread_outlen);
289 len =
c->decomp_size;
291 int ret = zlib_decomp(avctx, buf,
len, 0,
c->decomp_size);
295 encoded =
c->decomp_buf;
299 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
306 switch (
c->imgtype) {
309 for (row = 0; row <
height; row++) {
310 pixel_ptr = row *
width * 3;
311 yq = encoded[pixel_ptr++];
312 unsigned uqvq =
AV_RL16(encoded+pixel_ptr);
314 for (col = 1; col <
width; col++) {
315 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
316 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
317 AV_WL16(encoded+pixel_ptr+1, uqvq);
324 for (row = 0; row <
height; row++) {
326 for (col = 0; col <
width/4; col++) {
327 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
328 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
329 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
330 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
331 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
332 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
333 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
334 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
341 for (row = 0; row <
height; row++) {
343 for (col = 0; col <
width/4; col++) {
344 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
345 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
346 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
347 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
348 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
349 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
355 for (row = 0; row <
height; row++) {
356 pixel_ptr = row *
width * 2;
358 for (col = 0; col <
width/2; col++) {
359 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
360 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
361 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
362 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
368 for (row = 0; row <
height/2; row++) {
369 pixel_ptr = row *
width * 3;
370 yq = y1q = uq = vq =0;
371 for (col = 0; col <
width/2; col++) {
372 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
373 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
374 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
375 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
376 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
377 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
394 switch (
c->imgtype) {
396 for (row = 0; row <
height; row++) {
397 for (col = 0; col <
width; col++) {
398 y_out[col] = *encoded++;
399 u_out[col] = *encoded++ + 128;
400 v_out[col] = *encoded++ + 128;
402 y_out -=
frame->linesize[0];
403 u_out -=
frame->linesize[1];
404 v_out -=
frame->linesize[2];
408 for (row = 0; row <
height; row++) {
409 for (col = 0; col <
width - 3; col += 4) {
410 memcpy(y_out + col, encoded, 4);
412 u_out[ col >> 1 ] = *encoded++ + 128;
413 u_out[(col >> 1) + 1] = *encoded++ + 128;
414 v_out[ col >> 1 ] = *encoded++ + 128;
415 v_out[(col >> 1) + 1] = *encoded++ + 128;
417 if (col && col <
width) {
418 u_out[ col >> 1 ] = u_out[(col>>1) - 1];
419 v_out[ col >> 1 ] = v_out[(col>>1) - 1];
422 y_out -=
frame->linesize[0];
423 u_out -=
frame->linesize[1];
424 v_out -=
frame->linesize[2];
429 for (row =
height - 1; row >= 0; row--) {
430 pixel_ptr = row *
frame->linesize[0];
431 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
436 for (row = 0; row <
height; row++) {
437 for (col = 0; col <
width - 3; col += 4) {
438 memcpy(y_out + col, encoded, 4);
440 u_out[col >> 2] = *encoded++ + 128;
441 v_out[col >> 2] = *encoded++ + 128;
443 if (col && col <
width) {
444 u_out[col >> 2] = u_out[(col>>2) - 1];
445 v_out[col >> 2] = v_out[(col>>2) - 1];
447 y_out -=
frame->linesize[0];
448 u_out -=
frame->linesize[1];
449 v_out -=
frame->linesize[2];
453 for (row = 0; row <
height; row++) {
454 for (col = 0; col <
width - 1; col += 2) {
455 memcpy(y_out + col, encoded, 2);
457 u_out[col >> 1] = *encoded++ + 128;
458 v_out[col >> 1] = *encoded++ + 128;
460 y_out -=
frame->linesize[0];
461 u_out -=
frame->linesize[1];
462 v_out -=
frame->linesize[2];
468 for (row = 0; row <
height - 1; row += 2) {
469 for (col = 0; col <
width - 1; col += 2) {
470 memcpy(y_out + col, encoded, 2);
472 memcpy(y_out + col -
frame->linesize[0], encoded, 2);
474 u_out[col >> 1] = *encoded++ + 128;
475 v_out[col >> 1] = *encoded++ + 128;
477 y_out -=
frame->linesize[0] << 1;
478 u_out -=
frame->linesize[1];
479 v_out -=
frame->linesize[2];
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