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