00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029
00030 #include "libavutil/common.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "avcodec.h"
00033 #include "internal.h"
00034
00035 #include <zlib.h>
00036
00037 #define ZMBV_KEYFRAME 1
00038 #define ZMBV_DELTAPAL 2
00039
00040 #define ZMBV_BLOCK 16
00041
00045 typedef struct ZmbvEncContext {
00046 AVCodecContext *avctx;
00047 AVFrame pic;
00048
00049 int range;
00050 uint8_t *comp_buf, *work_buf;
00051 uint8_t pal[768];
00052 uint32_t pal2[256];
00053 uint8_t *prev;
00054 int pstride;
00055 int comp_size;
00056 int keyint, curfrm;
00057 z_stream zstream;
00058 } ZmbvEncContext;
00059
00060 static int score_tab[256];
00061
00066 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2,
00067 int bw, int bh, int *xored)
00068 {
00069 int sum = 0;
00070 int i, j;
00071 uint8_t histogram[256] = {0};
00072
00073 *xored = 0;
00074 for(j = 0; j < bh; j++){
00075 for(i = 0; i < bw; i++){
00076 int t = src[i] ^ src2[i];
00077 histogram[t]++;
00078 *xored |= t;
00079 }
00080 src += stride;
00081 src2 += stride2;
00082 }
00083
00084 for(i = 1; i < 256; i++)
00085 sum += score_tab[histogram[i]];
00086
00087 return sum;
00088 }
00089
00093 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
00094 int pstride, int x, int y, int *mx, int *my, int *xored)
00095 {
00096 int dx, dy, tx, ty, tv, bv, bw, bh;
00097
00098 *mx = *my = 0;
00099 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
00100 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
00101 bv = block_cmp(src, sstride, prev, pstride, bw, bh, xored);
00102 if(!bv) return 0;
00103 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
00104 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
00105 if(tx == x && ty == y) continue;
00106 dx = tx - x;
00107 dy = ty - y;
00108 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh, xored);
00109 if(tv < bv){
00110 bv = tv;
00111 *mx = dx;
00112 *my = dy;
00113 if(!bv) return 0;
00114 }
00115 }
00116 }
00117 return bv;
00118 }
00119
00120 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00121 const AVFrame *pict, int *got_packet)
00122 {
00123 ZmbvEncContext * const c = avctx->priv_data;
00124 AVFrame * const p = &c->pic;
00125 uint8_t *src, *prev, *buf;
00126 uint32_t *palptr;
00127 int keyframe, chpal;
00128 int fl;
00129 int work_size = 0, pkt_size;
00130 int bw, bh;
00131 int i, j, ret;
00132
00133 keyframe = !c->curfrm;
00134 c->curfrm++;
00135 if(c->curfrm == c->keyint)
00136 c->curfrm = 0;
00137 *p = *pict;
00138 p->pict_type= keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
00139 p->key_frame= keyframe;
00140 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
00141
00142 palptr = (uint32_t*)p->data[1];
00143 src = p->data[0];
00144 prev = c->prev;
00145 if(chpal){
00146 uint8_t tpal[3];
00147 for(i = 0; i < 256; i++){
00148 AV_WB24(tpal, palptr[i]);
00149 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
00150 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
00151 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
00152 c->pal[i * 3 + 0] = tpal[0];
00153 c->pal[i * 3 + 1] = tpal[1];
00154 c->pal[i * 3 + 2] = tpal[2];
00155 }
00156 memcpy(c->pal2, p->data[1], 1024);
00157 }
00158 if(keyframe){
00159 for(i = 0; i < 256; i++){
00160 AV_WB24(c->pal+(i*3), palptr[i]);
00161 }
00162 memcpy(c->work_buf, c->pal, 768);
00163 memcpy(c->pal2, p->data[1], 1024);
00164 work_size = 768;
00165 for(i = 0; i < avctx->height; i++){
00166 memcpy(c->work_buf + work_size, src, avctx->width);
00167 src += p->linesize[0];
00168 work_size += avctx->width;
00169 }
00170 }else{
00171 int x, y, bh2, bw2, xored;
00172 uint8_t *tsrc, *tprev;
00173 uint8_t *mv;
00174 int mx, my;
00175
00176 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
00177 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
00178 mv = c->work_buf + work_size;
00179 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
00180 work_size += (bw * bh * 2 + 3) & ~3;
00181
00182 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
00183 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
00184 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
00185 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
00186
00187 tsrc = src + x;
00188 tprev = prev + x;
00189
00190 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
00191 mv[0] = (mx << 1) | !!xored;
00192 mv[1] = my << 1;
00193 tprev += mx + my * c->pstride;
00194 if(xored){
00195 for(j = 0; j < bh2; j++){
00196 for(i = 0; i < bw2; i++)
00197 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
00198 tsrc += p->linesize[0];
00199 tprev += c->pstride;
00200 }
00201 }
00202 }
00203 src += p->linesize[0] * ZMBV_BLOCK;
00204 prev += c->pstride * ZMBV_BLOCK;
00205 }
00206 }
00207
00208 src = p->data[0];
00209 prev = c->prev;
00210 for(i = 0; i < avctx->height; i++){
00211 memcpy(prev, src, avctx->width);
00212 prev += c->pstride;
00213 src += p->linesize[0];
00214 }
00215
00216 if (keyframe)
00217 deflateReset(&c->zstream);
00218
00219 c->zstream.next_in = c->work_buf;
00220 c->zstream.avail_in = work_size;
00221 c->zstream.total_in = 0;
00222
00223 c->zstream.next_out = c->comp_buf;
00224 c->zstream.avail_out = c->comp_size;
00225 c->zstream.total_out = 0;
00226 if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
00227 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
00228 return -1;
00229 }
00230
00231 pkt_size = c->zstream.total_out + 1 + 6*keyframe;
00232 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size)) < 0)
00233 return ret;
00234 buf = pkt->data;
00235
00236 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
00237 *buf++ = fl;
00238 if (keyframe) {
00239 *buf++ = 0;
00240 *buf++ = 1;
00241 *buf++ = 1;
00242 *buf++ = 4;
00243 *buf++ = ZMBV_BLOCK;
00244 *buf++ = ZMBV_BLOCK;
00245 }
00246 memcpy(buf, c->comp_buf, c->zstream.total_out);
00247
00248 pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
00249 *got_packet = 1;
00250
00251 return 0;
00252 }
00253
00254
00258 static av_cold int encode_init(AVCodecContext *avctx)
00259 {
00260 ZmbvEncContext * const c = avctx->priv_data;
00261 int zret;
00262 int i;
00263 int lvl = 9;
00264
00265 for(i=1; i<256; i++)
00266 score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
00267
00268 c->avctx = avctx;
00269
00270 c->curfrm = 0;
00271 c->keyint = avctx->keyint_min;
00272 c->range = 8;
00273 if(avctx->me_range > 0)
00274 c->range = FFMIN(avctx->me_range, 127);
00275
00276 if(avctx->compression_level >= 0)
00277 lvl = avctx->compression_level;
00278 if(lvl < 0 || lvl > 9){
00279 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
00280 return AVERROR(EINVAL);
00281 }
00282
00283
00284 memset(&c->zstream, 0, sizeof(z_stream));
00285 c->comp_size = avctx->width * avctx->height + 1024 +
00286 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
00287 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
00288 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
00289 return AVERROR(ENOMEM);
00290 }
00291
00292 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
00293 ((c->comp_size + 63) >> 6) + 11;
00294
00295
00296 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
00297 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
00298 return AVERROR(ENOMEM);
00299 }
00300 c->pstride = FFALIGN(avctx->width, 16);
00301 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
00302 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
00303 return AVERROR(ENOMEM);
00304 }
00305
00306 c->zstream.zalloc = Z_NULL;
00307 c->zstream.zfree = Z_NULL;
00308 c->zstream.opaque = Z_NULL;
00309 zret = deflateInit(&c->zstream, lvl);
00310 if (zret != Z_OK) {
00311 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00312 return -1;
00313 }
00314
00315 avctx->coded_frame = &c->pic;
00316
00317 return 0;
00318 }
00319
00320
00321
00325 static av_cold int encode_end(AVCodecContext *avctx)
00326 {
00327 ZmbvEncContext * const c = avctx->priv_data;
00328
00329 av_freep(&c->comp_buf);
00330 av_freep(&c->work_buf);
00331
00332 deflateEnd(&c->zstream);
00333 av_freep(&c->prev);
00334
00335 return 0;
00336 }
00337
00338 AVCodec ff_zmbv_encoder = {
00339 .name = "zmbv",
00340 .type = AVMEDIA_TYPE_VIDEO,
00341 .id = AV_CODEC_ID_ZMBV,
00342 .priv_data_size = sizeof(ZmbvEncContext),
00343 .init = encode_init,
00344 .encode2 = encode_frame,
00345 .close = encode_end,
00346 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_PAL8, PIX_FMT_NONE },
00347 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
00348 };