51 #if CONFIG_ZLIB_DECODER
66 #if CONFIG_ZLIB_DECODER
76 static unsigned int mszh_decomp(
const unsigned char * srcptr,
int srclen,
unsigned char * destptr,
unsigned int destsize)
78 unsigned char *destptr_bak = destptr;
79 unsigned char *destptr_end = destptr + destsize;
80 const unsigned char *srcptr_end = srcptr + srclen;
81 unsigned mask = *srcptr++;
82 unsigned maskbit = 0x80;
84 while (srcptr < srcptr_end && destptr < destptr_end) {
85 if (!(
mask & maskbit)) {
86 memcpy(destptr, srcptr, 4);
90 unsigned ofs = bytestream_get_le16(&srcptr);
91 unsigned cnt = (ofs >> 11) + 1;
93 ofs =
FFMIN(ofs, destptr - destptr_bak);
95 cnt =
FFMIN(cnt, destptr_end - destptr);
101 memset(destptr, 0, cnt);
109 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32)
break;
110 memcpy(destptr, srcptr, 32);
119 return destptr - destptr_bak;
123 #if CONFIG_ZLIB_DECODER
134 int zret = inflateReset(&
c->zstream);
140 c->zstream.avail_in = src_len;
141 c->zstream.next_out =
c->decomp_buf +
offset;
142 c->zstream.avail_out =
c->decomp_size -
offset;
143 zret =
inflate(&
c->zstream, Z_FINISH);
144 if (zret != Z_OK && zret != Z_STREAM_END) {
148 if (expected != (
unsigned int)
c->zstream.total_out) {
150 expected,
c->zstream.total_out);
153 return c->zstream.total_out;
163 int buf_size = avpkt->
size;
165 unsigned int pixel_ptr;
167 unsigned char *encoded = avpkt->
data, *outptr;
168 uint8_t *y_out, *u_out, *v_out;
171 unsigned int mszh_dlen;
172 unsigned char yq, y1q, uq, vq;
174 unsigned int mthread_inlen, mthread_outlen;
175 unsigned int len = buf_size;
181 outptr =
frame->data[0];
186 switch (
c->compression) {
197 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
199 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
200 mszh_dlen =
mszh_decomp(
buf + 8, mthread_inlen,
c->decomp_buf,
c->decomp_size);
201 if (mthread_outlen != mszh_dlen) {
203 mthread_outlen, mszh_dlen);
207 c->decomp_buf + mthread_outlen,
c->decomp_size - mthread_outlen);
208 if (mthread_outlen != mszh_dlen) {
210 mthread_outlen, mszh_dlen);
213 encoded =
c->decomp_buf;
214 len =
c->decomp_size;
217 if (
c->decomp_size != mszh_dlen) {
219 c->decomp_size, mszh_dlen);
222 encoded =
c->decomp_buf;
228 switch (
c->imgtype) {
254 #if CONFIG_ZLIB_DECODER
262 memcpy(
c->decomp_buf,
buf,
len);
263 encoded =
c->decomp_buf;
269 mthread_inlen =
FFMIN(mthread_inlen,
len - 8);
271 mthread_outlen =
FFMIN(mthread_outlen,
c->decomp_size);
272 ret = zlib_decomp(avctx,
buf + 8, mthread_inlen, 0, mthread_outlen);
274 ret = zlib_decomp(avctx,
buf + 8 + mthread_inlen,
len - 8 - mthread_inlen,
275 mthread_outlen, mthread_outlen);
278 int ret = zlib_decomp(avctx,
buf,
len, 0,
c->decomp_size);
281 encoded =
c->decomp_buf;
282 len =
c->decomp_size;
286 av_log(avctx,
AV_LOG_ERROR,
"BUG! Unknown codec in frame decoder compression switch.\n");
293 switch (
c->imgtype) {
296 for (row = 0; row <
height; row++) {
297 pixel_ptr = row *
width * 3;
298 yq = encoded[pixel_ptr++];
299 uqvq =
AV_RL16(encoded+pixel_ptr);
301 for (col = 1; col <
width; col++) {
302 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
303 uqvq -=
AV_RL16(encoded+pixel_ptr+1);
304 AV_WL16(encoded+pixel_ptr+1, uqvq);
310 for (row = 0; row <
height; row++) {
311 pixel_ptr = row *
width * 2;
313 for (col = 0; col <
width/4; col++) {
314 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
315 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
316 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
317 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
318 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
319 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
320 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
321 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
327 for (row = 0; row <
height; row++) {
328 pixel_ptr = row *
width / 2 * 3;
330 for (col = 0; col <
width/4; col++) {
331 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
332 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
333 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
334 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
335 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
336 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
342 for (row = 0; row <
height; row++) {
343 pixel_ptr = row *
width * 2;
345 for (col = 0; col <
width/2; col++) {
346 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
347 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
348 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
349 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
355 for (row = 0; row <
height/2; row++) {
356 pixel_ptr = row *
width * 3;
357 yq = y1q = uq = vq =0;
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] = y1q -= encoded[pixel_ptr+2];
362 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
363 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
364 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
379 switch (
c->imgtype) {
381 for (row = 0; row <
height; row++) {
382 for (col = 0; col <
width; col++) {
383 y_out[col] = *encoded++;
384 u_out[col] = *encoded++ + 128;
385 v_out[col] = *encoded++ + 128;
387 y_out -=
frame->linesize[0];
388 u_out -=
frame->linesize[1];
389 v_out -=
frame->linesize[2];
393 for (row = 0; row <
height; row++) {
394 for (col = 0; col <
width - 3; col += 4) {
395 memcpy(y_out + col, encoded, 4);
397 u_out[ col >> 1 ] = *encoded++ + 128;
398 u_out[(col >> 1) + 1] = *encoded++ + 128;
399 v_out[ col >> 1 ] = *encoded++ + 128;
400 v_out[(col >> 1) + 1] = *encoded++ + 128;
402 y_out -=
frame->linesize[0];
403 u_out -=
frame->linesize[1];
404 v_out -=
frame->linesize[2];
409 for (row =
height - 1; row >= 0; row--) {
410 pixel_ptr = row *
frame->linesize[0];
411 memcpy(outptr + pixel_ptr, encoded, 3 *
width);
416 for (row = 0; row <
height; row++) {
417 for (col = 0; col <
width - 3; col += 4) {
418 memcpy(y_out + col, encoded, 4);
420 u_out[col >> 2] = *encoded++ + 128;
421 v_out[col >> 2] = *encoded++ + 128;
423 y_out -=
frame->linesize[0];
424 u_out -=
frame->linesize[1];
425 v_out -=
frame->linesize[2];
429 for (row = 0; row <
height; row++) {
430 for (col = 0; col <
width - 1; col += 2) {
431 memcpy(y_out + col, encoded, 2);
433 u_out[col >> 1] = *encoded++ + 128;
434 v_out[col >> 1] = *encoded++ + 128;
436 y_out -=
frame->linesize[0];
437 u_out -=
frame->linesize[1];
438 v_out -=
frame->linesize[2];
444 for (row = 0; row <
height - 1; row += 2) {
445 for (col = 0; col <
width - 1; col += 2) {
446 memcpy(y_out + col, encoded, 2);
448 memcpy(y_out + col -
frame->linesize[0], encoded, 2);
450 u_out[col >> 1] = *encoded++ + 128;
451 v_out[col >> 1] = *encoded++ + 128;
453 y_out -=
frame->linesize[0] << 1;
454 u_out -=
frame->linesize[1];
455 v_out -=
frame->linesize[2];
463 frame->key_frame = 1;
475 unsigned int basesize = avctx->
width * avctx->
height;
478 unsigned int max_decomp_size;
479 int subsample_h, subsample_v;
489 av_log(avctx,
AV_LOG_ERROR,
"Codec id and codec type mismatch. This should not happen.\n");
495 c->decomp_size = basesize * 3;
496 max_decomp_size = max_basesize * 3;
501 c->decomp_size = basesize * 2;
502 max_decomp_size = max_basesize * 2;
505 if (avctx->
width % 4) {
511 c->decomp_size = basesize * 3;
512 max_decomp_size = max_basesize * 3;
517 c->decomp_size = basesize / 2 * 3;
518 max_decomp_size = max_basesize / 2 * 3;
523 c->decomp_size = basesize * 2;
524 max_decomp_size = max_basesize * 2;
529 c->decomp_size = basesize / 2 * 3;
530 max_decomp_size = max_basesize / 2 * 3;
540 if (avctx->
width % (1<<subsample_h) || avctx->
height % (1<<subsample_v)) {
549 switch (
c->compression) {
558 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression format for MSZH (%d).\n",
c->compression);
562 #if CONFIG_ZLIB_DECODER
564 switch (
c->compression) {
575 if (
c->compression < Z_NO_COMPRESSION ||
c->compression > Z_BEST_COMPRESSION) {
576 av_log(avctx,
AV_LOG_ERROR,
"Unsupported compression level for ZLIB: (%d).\n",
c->compression);
589 if (
c->decomp_size) {
590 if (!(
c->decomp_buf =
av_malloc(max_decomp_size))) {
608 #if CONFIG_ZLIB_DECODER
611 c->zstream.zalloc = Z_NULL;
612 c->zstream.zfree = Z_NULL;
613 c->zstream.opaque = Z_NULL;
614 zret = inflateInit(&
c->zstream);
638 #if CONFIG_ZLIB_DECODER
640 inflateEnd(&
c->zstream);
646 #if CONFIG_MSZH_DECODER
662 #if CONFIG_ZLIB_DECODER