2 * Zip Motion Blocks Video (ZMBV) encoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Zip Motion Blocks Video encoder
36 #define ZMBV_KEYFRAME 1
37 #define ZMBV_DELTAPAL 2
44 typedef struct ZmbvEncContext {
45 AVCodecContext *avctx;
49 uint8_t *comp_buf, *work_buf;
51 uint32_t pal2[256]; //for quick comparisons
59 /** Block comparing function
60 * XXX should be optimized and moved to DSPContext
61 * TODO handle out of edge ME
63 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
68 for(j = 0; j < bh; j++){
69 for(i = 0; i < bw; i++)
70 sum += src[i] ^ src2[i];
77 /** Motion estimation function
78 * TODO make better ME decisions
80 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
81 int x, int y, int *mx, int *my)
83 int dx, dy, tx, ty, tv, bv, bw, bh;
86 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
87 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
88 bv = block_cmp(src, sstride, prev, pstride, bw, bh);
90 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
91 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
92 if(tx == x && ty == y) continue; // we already tested this block
95 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh);
107 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
109 ZmbvEncContext * const c = avctx->priv_data;
110 AVFrame *pict = data;
111 AVFrame * const p = &c->pic;
122 keyframe = !c->curfrm;
124 if(c->curfrm == c->keyint)
127 p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
128 p->key_frame= keyframe;
129 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
131 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
134 deflateReset(&c->zstream);
135 *buf++ = 0; len++; // hi ver
136 *buf++ = 1; len++; // lo ver
137 *buf++ = 1; len++; // comp
138 *buf++ = 4; len++; // format - 8bpp
139 *buf++ = ZMBV_BLOCK; len++; // block width
140 *buf++ = ZMBV_BLOCK; len++; // block height
142 palptr = (uint32_t*)p->data[1];
147 for(i = 0; i < 256; i++){
148 tpal[0] = palptr[i] >> 16;
149 tpal[1] = palptr[i] >> 8;
151 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
152 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
153 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
154 c->pal[i * 3 + 0] = tpal[0];
155 c->pal[i * 3 + 1] = tpal[1];
156 c->pal[i * 3 + 2] = tpal[2];
158 memcpy(c->pal2, p->data[1], 1024);
161 for(i = 0; i < 256; i++){
162 c->pal[i*3 + 0] = palptr[i] >> 16;
163 c->pal[i*3 + 1] = palptr[i] >> 8;
164 c->pal[i*3 + 2] = palptr[i];
166 memcpy(c->work_buf, c->pal, 768);
167 memcpy(c->pal2, p->data[1], 1024);
169 for(i = 0; i < avctx->height; i++){
170 memcpy(c->work_buf + work_size, src, avctx->width);
171 src += p->linesize[0];
172 work_size += avctx->width;
176 uint8_t *tsrc, *tprev;
180 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
181 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
182 mv = c->work_buf + work_size;
183 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
184 work_size += (bw * bh * 2 + 3) & ~3;
185 /* for now just XOR'ing */
186 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
187 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
188 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
189 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
194 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
195 mv[0] = (mx << 1) | !!bv;
197 tprev += mx + my * c->pstride;
199 for(j = 0; j < bh2; j++){
200 for(i = 0; i < bw2; i++)
201 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
202 tsrc += p->linesize[0];
207 src += p->linesize[0] * ZMBV_BLOCK;
208 prev += c->pstride * ZMBV_BLOCK;
211 /* save the previous frame */
214 for(i = 0; i < avctx->height; i++){
215 memcpy(prev, src, avctx->width);
217 src += p->linesize[0];
220 c->zstream.next_in = c->work_buf;
221 c->zstream.avail_in = work_size;
222 c->zstream.total_in = 0;
224 c->zstream.next_out = c->comp_buf;
225 c->zstream.avail_out = c->comp_size;
226 c->zstream.total_out = 0;
227 if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
228 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
232 memcpy(buf, c->comp_buf, c->zstream.total_out);
233 return len + c->zstream.total_out;
240 static int encode_init(AVCodecContext *avctx)
242 ZmbvEncContext * const c = avctx->priv_data;
243 int zret; // Zlib return code
248 c->pic.data[0] = NULL;
250 c->keyint = avctx->keyint_min;
252 if(avctx->me_range > 0)
253 c->range = FFMIN(avctx->me_range, 127);
255 if(avctx->compression_level >= 0)
256 lvl = avctx->compression_level;
257 if(lvl < 0 || lvl > 9){
258 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
262 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
266 // Needed if zlib unused or init aborted before deflateInit
267 memset(&(c->zstream), 0, sizeof(z_stream));
268 c->comp_size = avctx->width * avctx->height + 1024 +
269 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
270 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
271 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
274 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
275 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
276 ((c->comp_size + 63) >> 6) + 11;
278 /* Allocate compression buffer */
279 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
280 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
283 c->pstride = (avctx->width + 15) & ~15;
284 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
285 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
289 c->zstream.zalloc = Z_NULL;
290 c->zstream.zfree = Z_NULL;
291 c->zstream.opaque = Z_NULL;
292 zret = deflateInit(&(c->zstream), lvl);
294 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
304 * Uninit zmbv encoder
306 static int encode_end(AVCodecContext *avctx)
308 ZmbvEncContext * const c = avctx->priv_data;
310 av_freep(&c->comp_buf);
311 av_freep(&c->work_buf);
313 deflateEnd(&(c->zstream));
319 AVCodec zmbv_encoder = {
323 sizeof(ZmbvEncContext),
327 .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},