00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042
00043 #include "common.h"
00044 #include "bitstream.h"
00045 #include "avcodec.h"
00046
00047 #ifdef CONFIG_ZLIB
00048 #include <zlib.h>
00049 #endif
00050
00051
00052 #define BMPTYPE_YUV 1
00053 #define BMPTYPE_RGB 2
00054
00055 #define IMGTYPE_YUV111 0
00056 #define IMGTYPE_YUV422 1
00057 #define IMGTYPE_RGB24 2
00058 #define IMGTYPE_YUV411 3
00059 #define IMGTYPE_YUV211 4
00060 #define IMGTYPE_YUV420 5
00061
00062 #define COMP_MSZH 0
00063 #define COMP_MSZH_NOCOMP 1
00064 #define COMP_ZLIB_HISPEED 1
00065 #define COMP_ZLIB_HICOMP 9
00066 #define COMP_ZLIB_NORMAL -1
00067
00068 #define FLAG_MULTITHREAD 1
00069 #define FLAG_NULLFRAME 2
00070 #define FLAG_PNGFILTER 4
00071 #define FLAGMASK_UNUSED 0xf8
00072
00073 #define CODEC_MSZH 1
00074 #define CODEC_ZLIB 3
00075
00076 #define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
00077 #define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
00078
00079
00080
00081
00082 typedef struct LclContext {
00083
00084 AVCodecContext *avctx;
00085 AVFrame pic;
00086 PutBitContext pb;
00087
00088
00089 int imgtype;
00090
00091 int compression;
00092
00093 int flags;
00094
00095 unsigned int decomp_size;
00096
00097 unsigned char* decomp_buf;
00098
00099 unsigned int max_comp_size;
00100
00101 unsigned char* comp_buf;
00102 #ifdef CONFIG_ZLIB
00103 z_stream zstream;
00104 #endif
00105 } LclContext;
00106
00107
00108
00109
00110
00111
00112
00113 static inline unsigned char fix (int pix14)
00114 {
00115 int tmp;
00116
00117 tmp = (pix14 + 0x80000) >> 20;
00118 if (tmp < 0)
00119 return 0;
00120 if (tmp > 255)
00121 return 255;
00122 return tmp;
00123 }
00124
00125
00126
00127 static inline unsigned char get_b (unsigned char yq, signed char bq)
00128 {
00129 return fix((yq << 20) + bq * 1858076);
00130 }
00131
00132
00133
00134 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
00135 {
00136 return fix((yq << 20) - bq * 360857 - rq * 748830);
00137 }
00138
00139
00140
00141 static inline unsigned char get_r (unsigned char yq, signed char rq)
00142 {
00143 return fix((yq << 20) + rq * 1470103);
00144 }
00145
00146
00147
00148 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
00149 {
00150 unsigned char *destptr_bak = destptr;
00151 unsigned char *destptr_end = destptr + destsize;
00152 unsigned char mask = 0;
00153 unsigned char maskbit = 0;
00154 unsigned int ofs, cnt;
00155
00156 while ((srclen > 0) && (destptr < destptr_end)) {
00157 if (maskbit == 0) {
00158 mask = *(srcptr++);
00159 maskbit = 8;
00160 srclen--;
00161 continue;
00162 }
00163 if ((mask & (1 << (--maskbit))) == 0) {
00164 if (destptr + 4 > destptr_end)
00165 break;
00166 *(int*)destptr = *(int*)srcptr;
00167 srclen -= 4;
00168 destptr += 4;
00169 srcptr += 4;
00170 } else {
00171 ofs = *(srcptr++);
00172 cnt = *(srcptr++);
00173 ofs += cnt * 256;;
00174 cnt = ((cnt >> 3) & 0x1f) + 1;
00175 ofs &= 0x7ff;
00176 srclen -= 2;
00177 cnt *= 4;
00178 if (destptr + cnt > destptr_end) {
00179 cnt = destptr_end - destptr;
00180 }
00181 for (; cnt > 0; cnt--) {
00182 *(destptr) = *(destptr - ofs);
00183 destptr++;
00184 }
00185 }
00186 }
00187
00188 return (destptr - destptr_bak);
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
00200 {
00201 LclContext * const c = (LclContext *)avctx->priv_data;
00202 unsigned char *encoded = (unsigned char *)buf;
00203 unsigned int pixel_ptr;
00204 int row, col;
00205 unsigned char *outptr;
00206 unsigned int width = avctx->width;
00207 unsigned int height = avctx->height;
00208 unsigned int mszh_dlen;
00209 unsigned char yq, y1q, uq, vq;
00210 int uqvq;
00211 unsigned int mthread_inlen, mthread_outlen;
00212 #ifdef CONFIG_ZLIB
00213 int zret;
00214 #endif
00215 unsigned int len = buf_size;
00216
00217 if(c->pic.data[0])
00218 avctx->release_buffer(avctx, &c->pic);
00219
00220 c->pic.reference = 0;
00221 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00222 if(avctx->get_buffer(avctx, &c->pic) < 0){
00223 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00224 return -1;
00225 }
00226
00227 outptr = c->pic.data[0];
00228
00229
00230 switch (avctx->codec_id) {
00231 case CODEC_ID_MSZH:
00232 switch (c->compression) {
00233 case COMP_MSZH:
00234 if (c->flags & FLAG_MULTITHREAD) {
00235 mthread_inlen = *((unsigned int*)encoded);
00236 mthread_outlen = *((unsigned int*)(encoded+4));
00237 if (mthread_outlen > c->decomp_size)
00238 mthread_outlen = c->decomp_size;
00239 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
00240 if (mthread_outlen != mszh_dlen) {
00241 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
00242 mthread_outlen, mszh_dlen);
00243 return -1;
00244 }
00245 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
00246 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
00247 if (mthread_outlen != mszh_dlen) {
00248 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
00249 mthread_outlen, mszh_dlen);
00250 return -1;
00251 }
00252 encoded = c->decomp_buf;
00253 len = c->decomp_size;
00254 } else {
00255 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
00256 if (c->decomp_size != mszh_dlen) {
00257 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
00258 c->decomp_size, mszh_dlen);
00259 return -1;
00260 }
00261 encoded = c->decomp_buf;
00262 len = mszh_dlen;
00263 }
00264 break;
00265 case COMP_MSZH_NOCOMP:
00266 break;
00267 default:
00268 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
00269 return -1;
00270 }
00271 break;
00272 case CODEC_ID_ZLIB:
00273 #ifdef CONFIG_ZLIB
00274
00275
00276
00277 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
00278 (len == width * height * 3))
00279 break;
00280 zret = inflateReset(&(c->zstream));
00281 if (zret != Z_OK) {
00282 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00283 return -1;
00284 }
00285 if (c->flags & FLAG_MULTITHREAD) {
00286 mthread_inlen = *((unsigned int*)encoded);
00287 mthread_outlen = *((unsigned int*)(encoded+4));
00288 if (mthread_outlen > c->decomp_size)
00289 mthread_outlen = c->decomp_size;
00290 c->zstream.next_in = encoded + 8;
00291 c->zstream.avail_in = mthread_inlen;
00292 c->zstream.next_out = c->decomp_buf;
00293 c->zstream.avail_out = c->decomp_size;
00294 zret = inflate(&(c->zstream), Z_FINISH);
00295 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
00296 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
00297 return -1;
00298 }
00299 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
00300 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
00301 mthread_outlen, c->zstream.total_out);
00302 return -1;
00303 }
00304 zret = inflateReset(&(c->zstream));
00305 if (zret != Z_OK) {
00306 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
00307 return -1;
00308 }
00309 c->zstream.next_in = encoded + 8 + mthread_inlen;
00310 c->zstream.avail_in = len - mthread_inlen;
00311 c->zstream.next_out = c->decomp_buf + mthread_outlen;
00312 c->zstream.avail_out = c->decomp_size - mthread_outlen;
00313 zret = inflate(&(c->zstream), Z_FINISH);
00314 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
00315 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
00316 return -1;
00317 }
00318 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
00319 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
00320 mthread_outlen, c->zstream.total_out);
00321 return -1;
00322 }
00323 } else {
00324 c->zstream.next_in = encoded;
00325 c->zstream.avail_in = len;
00326 c->zstream.next_out = c->decomp_buf;
00327 c->zstream.avail_out = c->decomp_size;
00328 zret = inflate(&(c->zstream), Z_FINISH);
00329 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
00330 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
00331 return -1;
00332 }
00333 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
00334 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
00335 c->decomp_size, c->zstream.total_out);
00336 return -1;
00337 }
00338 }
00339 encoded = c->decomp_buf;
00340 len = c->decomp_size;;
00341 #else
00342 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
00343 return -1;
00344 #endif
00345 break;
00346 default:
00347 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
00348 return -1;
00349 }
00350
00351
00352
00353 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
00354 switch (c->imgtype) {
00355 case IMGTYPE_YUV111:
00356 case IMGTYPE_RGB24:
00357 for (row = 0; row < height; row++) {
00358 pixel_ptr = row * width * 3;
00359 yq = encoded[pixel_ptr++];
00360 uqvq = encoded[pixel_ptr++];
00361 uqvq+=(encoded[pixel_ptr++] << 8);
00362 for (col = 1; col < width; col++) {
00363 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00364 uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8));
00365 encoded[pixel_ptr+1] = (uqvq) & 0xff;
00366 encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff;
00367 pixel_ptr += 3;
00368 }
00369 }
00370 break;
00371 case IMGTYPE_YUV422:
00372 for (row = 0; row < height; row++) {
00373 pixel_ptr = row * width * 2;
00374 yq = uq = vq =0;
00375 for (col = 0; col < width/4; col++) {
00376 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00377 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00378 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
00379 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
00380 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
00381 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
00382 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
00383 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
00384 pixel_ptr += 8;
00385 }
00386 }
00387 break;
00388 case IMGTYPE_YUV411:
00389 for (row = 0; row < height; row++) {
00390 pixel_ptr = row * width / 2 * 3;
00391 yq = uq = vq =0;
00392 for (col = 0; col < width/4; col++) {
00393 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00394 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00395 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
00396 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
00397 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
00398 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
00399 pixel_ptr += 6;
00400 }
00401 }
00402 break;
00403 case IMGTYPE_YUV211:
00404 for (row = 0; row < height; row++) {
00405 pixel_ptr = row * width * 2;
00406 yq = uq = vq =0;
00407 for (col = 0; col < width/2; col++) {
00408 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00409 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00410 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
00411 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
00412 pixel_ptr += 4;
00413 }
00414 }
00415 break;
00416 case IMGTYPE_YUV420:
00417 for (row = 0; row < height/2; row++) {
00418 pixel_ptr = row * width * 3;
00419 yq = y1q = uq = vq =0;
00420 for (col = 0; col < width/2; col++) {
00421 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00422 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00423 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
00424 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
00425 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
00426 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
00427 pixel_ptr += 6;
00428 }
00429 }
00430 break;
00431 default:
00432 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
00433 return -1;
00434 }
00435 }
00436
00437
00438 switch (c->imgtype) {
00439 case IMGTYPE_YUV111:
00440 for (row = height - 1; row >= 0; row--) {
00441 pixel_ptr = row * c->pic.linesize[0];
00442 for (col = 0; col < width; col++) {
00443 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
00444 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
00445 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
00446 encoded += 3;
00447 }
00448 }
00449 break;
00450 case IMGTYPE_YUV422:
00451 for (row = height - 1; row >= 0; row--) {
00452 pixel_ptr = row * c->pic.linesize[0];
00453 for (col = 0; col < width/4; col++) {
00454 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
00455 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
00456 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
00457 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
00458 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
00459 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
00460 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
00461 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
00462 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
00463 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
00464 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
00465 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
00466 encoded += 8;
00467 }
00468 }
00469 break;
00470 case IMGTYPE_RGB24:
00471 for (row = height - 1; row >= 0; row--) {
00472 pixel_ptr = row * c->pic.linesize[0];
00473 for (col = 0; col < width; col++) {
00474 outptr[pixel_ptr++] = encoded[0];
00475 outptr[pixel_ptr++] = encoded[1];
00476 outptr[pixel_ptr++] = encoded[2];
00477 encoded += 3;
00478 }
00479 }
00480 break;
00481 case IMGTYPE_YUV411:
00482 for (row = height - 1; row >= 0; row--) {
00483 pixel_ptr = row * c->pic.linesize[0];
00484 for (col = 0; col < width/4; col++) {
00485 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
00486 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
00487 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
00488 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
00489 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
00490 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
00491 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
00492 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
00493 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
00494 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
00495 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
00496 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
00497 encoded += 6;
00498 }
00499 }
00500 break;
00501 case IMGTYPE_YUV211:
00502 for (row = height - 1; row >= 0; row--) {
00503 pixel_ptr = row * c->pic.linesize[0];
00504 for (col = 0; col < width/2; col++) {
00505 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
00506 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
00507 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
00508 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
00509 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
00510 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
00511 encoded += 4;
00512 }
00513 }
00514 break;
00515 case IMGTYPE_YUV420:
00516 for (row = height / 2 - 1; row >= 0; row--) {
00517 pixel_ptr = 2 * row * c->pic.linesize[0];
00518 for (col = 0; col < width/2; col++) {
00519 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
00520 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
00521 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
00522 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
00523 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
00524 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
00525 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
00526 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
00527 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
00528 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
00529 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
00530 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
00531 pixel_ptr += 6;
00532 encoded += 6;
00533 }
00534 }
00535 break;
00536 default:
00537 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
00538 return -1;
00539 }
00540
00541 *data_size = sizeof(AVFrame);
00542 *(AVFrame*)data = c->pic;
00543
00544
00545 return buf_size;
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00556 LclContext *c = avctx->priv_data;
00557 AVFrame *pict = data;
00558 AVFrame * const p = &c->pic;
00559 int i;
00560 int zret;
00561
00562 #ifndef CONFIG_ZLIB
00563 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
00564 return -1;
00565 #else
00566
00567 init_put_bits(&c->pb, buf, buf_size);
00568
00569 *p = *pict;
00570 p->pict_type= FF_I_TYPE;
00571 p->key_frame= 1;
00572
00573 if(avctx->pix_fmt != PIX_FMT_BGR24){
00574 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
00575 return -1;
00576 }
00577
00578 zret = deflateReset(&(c->zstream));
00579 if (zret != Z_OK) {
00580 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
00581 return -1;
00582 }
00583 c->zstream.next_out = c->comp_buf;
00584 c->zstream.avail_out = c->max_comp_size;
00585
00586 for(i = avctx->height - 1; i >= 0; i--) {
00587 c->zstream.next_in = p->data[0]+p->linesize[0]*i;
00588 c->zstream.avail_in = avctx->width*3;
00589 zret = deflate(&(c->zstream), Z_NO_FLUSH);
00590 if (zret != Z_OK) {
00591 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
00592 return -1;
00593 }
00594 }
00595 zret = deflate(&(c->zstream), Z_FINISH);
00596 if (zret != Z_STREAM_END) {
00597 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
00598 return -1;
00599 }
00600
00601 for (i = 0; i < c->zstream.total_out; i++)
00602 put_bits(&c->pb, 8, c->comp_buf[i]);
00603 flush_put_bits(&c->pb);
00604
00605 return c->zstream.total_out;
00606 #endif
00607 }
00608
00609
00610
00611
00612
00613
00614
00615
00616 static int decode_init(AVCodecContext *avctx)
00617 {
00618 LclContext * const c = (LclContext *)avctx->priv_data;
00619 unsigned int basesize = avctx->width * avctx->height;
00620 unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
00621 unsigned int max_decomp_size;
00622 int zret;
00623
00624 c->avctx = avctx;
00625 avctx->has_b_frames = 0;
00626
00627 c->pic.data[0] = NULL;
00628
00629 #ifdef CONFIG_ZLIB
00630
00631 memset(&(c->zstream), 0, sizeof(z_stream));
00632 #endif
00633
00634 if (avctx->extradata_size < 8) {
00635 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
00636 return 1;
00637 }
00638
00639 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
00640 return 1;
00641 }
00642
00643
00644 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
00645 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
00646 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
00647 }
00648
00649
00650 switch (c->imgtype = *((char *)avctx->extradata + 4)) {
00651 case IMGTYPE_YUV111:
00652 c->decomp_size = basesize * 3;
00653 max_decomp_size = max_basesize * 3;
00654 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
00655 break;
00656 case IMGTYPE_YUV422:
00657 c->decomp_size = basesize * 2;
00658 max_decomp_size = max_basesize * 2;
00659 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
00660 break;
00661 case IMGTYPE_RGB24:
00662 c->decomp_size = basesize * 3;
00663 max_decomp_size = max_basesize * 3;
00664 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
00665 break;
00666 case IMGTYPE_YUV411:
00667 c->decomp_size = basesize / 2 * 3;
00668 max_decomp_size = max_basesize / 2 * 3;
00669 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
00670 break;
00671 case IMGTYPE_YUV211:
00672 c->decomp_size = basesize * 2;
00673 max_decomp_size = max_basesize * 2;
00674 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
00675 break;
00676 case IMGTYPE_YUV420:
00677 c->decomp_size = basesize / 2 * 3;
00678 max_decomp_size = max_basesize / 2 * 3;
00679 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
00680 break;
00681 default:
00682 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
00683 return 1;
00684 }
00685
00686
00687 c->compression = *((char *)avctx->extradata + 5);
00688 switch (avctx->codec_id) {
00689 case CODEC_ID_MSZH:
00690 switch (c->compression) {
00691 case COMP_MSZH:
00692 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
00693 break;
00694 case COMP_MSZH_NOCOMP:
00695 c->decomp_size = 0;
00696 av_log(avctx, AV_LOG_INFO, "No compression.\n");
00697 break;
00698 default:
00699 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
00700 return 1;
00701 }
00702 break;
00703 case CODEC_ID_ZLIB:
00704 #ifdef CONFIG_ZLIB
00705 switch (c->compression) {
00706 case COMP_ZLIB_HISPEED:
00707 av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
00708 break;
00709 case COMP_ZLIB_HICOMP:
00710 av_log(avctx, AV_LOG_INFO, "High compression.\n");
00711 break;
00712 case COMP_ZLIB_NORMAL:
00713 av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
00714 break;
00715 default:
00716 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
00717 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
00718 return 1;
00719 }
00720 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
00721 }
00722 #else
00723 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
00724 return 1;
00725 #endif
00726 break;
00727 default:
00728 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
00729 return 1;
00730 }
00731
00732
00733 if (c->decomp_size) {
00734 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
00735 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00736 return 1;
00737 }
00738 }
00739
00740
00741 c->flags = *((char *)avctx->extradata + 6);
00742 if (c->flags & FLAG_MULTITHREAD)
00743 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
00744 if (c->flags & FLAG_NULLFRAME)
00745 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
00746 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
00747 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
00748 if (c->flags & FLAGMASK_UNUSED)
00749 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
00750
00751
00752 if (avctx->codec_id == CODEC_ID_ZLIB) {
00753 #ifdef CONFIG_ZLIB
00754 c->zstream.zalloc = Z_NULL;
00755 c->zstream.zfree = Z_NULL;
00756 c->zstream.opaque = Z_NULL;
00757 zret = inflateInit(&(c->zstream));
00758 if (zret != Z_OK) {
00759 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00760 return 1;
00761 }
00762 #else
00763 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
00764 return 1;
00765 #endif
00766 }
00767
00768 avctx->pix_fmt = PIX_FMT_BGR24;
00769
00770 return 0;
00771 }
00772
00773
00774
00775
00776
00777
00778
00779
00780 static int encode_init(AVCodecContext *avctx)
00781 {
00782 LclContext *c = avctx->priv_data;
00783 int zret;
00784
00785 #ifndef CONFIG_ZLIB
00786 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
00787 return 1;
00788 #else
00789
00790 c->avctx= avctx;
00791
00792 assert(avctx->width && avctx->height);
00793
00794 avctx->extradata= av_mallocz(8);
00795 avctx->coded_frame= &c->pic;
00796
00797
00798 c->compression = 6;
00799 c->flags = 0;
00800
00801 switch(avctx->pix_fmt){
00802 case PIX_FMT_BGR24:
00803 c->imgtype = IMGTYPE_RGB24;
00804 c->decomp_size = avctx->width * avctx->height * 3;
00805 avctx->bits_per_sample= 24;
00806 break;
00807 default:
00808 av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
00809 return -1;
00810 }
00811
00812 ((uint8_t*)avctx->extradata)[0]= 4;
00813 ((uint8_t*)avctx->extradata)[1]= 0;
00814 ((uint8_t*)avctx->extradata)[2]= 0;
00815 ((uint8_t*)avctx->extradata)[3]= 0;
00816 ((uint8_t*)avctx->extradata)[4]= c->imgtype;
00817 ((uint8_t*)avctx->extradata)[5]= c->compression;
00818 ((uint8_t*)avctx->extradata)[6]= c->flags;
00819 ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
00820 c->avctx->extradata_size= 8;
00821
00822 c->zstream.zalloc = Z_NULL;
00823 c->zstream.zfree = Z_NULL;
00824 c->zstream.opaque = Z_NULL;
00825 zret = deflateInit(&(c->zstream), c->compression);
00826 if (zret != Z_OK) {
00827 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
00828 return 1;
00829 }
00830
00831
00832 c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
00833 ((c->decomp_size + 63) >> 6) + 11;
00834 if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
00835 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
00836 return 1;
00837 }
00838
00839 return 0;
00840 #endif
00841 }
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852 static int decode_end(AVCodecContext *avctx)
00853 {
00854 LclContext * const c = (LclContext *)avctx->priv_data;
00855
00856 if (c->pic.data[0])
00857 avctx->release_buffer(avctx, &c->pic);
00858 #ifdef CONFIG_ZLIB
00859 inflateEnd(&(c->zstream));
00860 #endif
00861
00862 return 0;
00863 }
00864
00865
00866
00867
00868
00869
00870
00871
00872 static int encode_end(AVCodecContext *avctx)
00873 {
00874 LclContext *c = avctx->priv_data;
00875
00876 av_freep(&avctx->extradata);
00877 av_freep(&c->comp_buf);
00878 #ifdef CONFIG_ZLIB
00879 deflateEnd(&(c->zstream));
00880 #endif
00881
00882 return 0;
00883 }
00884
00885 AVCodec mszh_decoder = {
00886 "mszh",
00887 CODEC_TYPE_VIDEO,
00888 CODEC_ID_MSZH,
00889 sizeof(LclContext),
00890 decode_init,
00891 NULL,
00892 decode_end,
00893 decode_frame,
00894 CODEC_CAP_DR1,
00895 };
00896
00897
00898 AVCodec zlib_decoder = {
00899 "zlib",
00900 CODEC_TYPE_VIDEO,
00901 CODEC_ID_ZLIB,
00902 sizeof(LclContext),
00903 decode_init,
00904 NULL,
00905 decode_end,
00906 decode_frame,
00907 CODEC_CAP_DR1,
00908 };
00909
00910 #ifdef CONFIG_ENCODERS
00911
00912 AVCodec zlib_encoder = {
00913 "zlib",
00914 CODEC_TYPE_VIDEO,
00915 CODEC_ID_ZLIB,
00916 sizeof(LclContext),
00917 encode_init,
00918 encode_frame,
00919 encode_end,
00920 };
00921
00922 #endif //CONFIG_ENCODERS