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