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
24 * Zip Motion Blocks Video encoder
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
37 /* Frame header flags */
38 #define ZMBV_KEYFRAME 1
39 #define ZMBV_DELTAPAL 2
41 /* Motion block width/height (maximum allowed value is 255)
42 * Note: histogram datatype in block_cmp() must be big enough to hold values
43 * up to (4 * ZMBV_BLOCK * ZMBV_BLOCK)
47 /* Keyframe header format values */
63 typedef struct ZmbvEncContext {
64 AVCodecContext *avctx;
67 uint8_t *comp_buf, *work_buf;
69 uint32_t pal2[256]; //for quick comparisons
70 uint8_t *prev, *prev_buf;
78 int score_tab[ZMBV_BLOCK * ZMBV_BLOCK * 4 + 1];
82 /** Block comparing function
83 * XXX should be optimized and moved to DSPContext
85 static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
86 uint8_t *src2, int stride2, int bw, int bh,
91 uint16_t histogram[256] = {0};
92 int bw_bytes = bw * c->bypp;
94 /* Build frequency histogram of byte values for src[] ^ src2[] */
95 for(j = 0; j < bh; j++){
96 for(i = 0; i < bw_bytes; i++){
97 int t = src[i] ^ src2[i];
104 /* If not all the xored values were 0, then the blocks are different */
105 *xored = (histogram[0] < bw_bytes * bh);
107 /* Exit early if blocks are equal */
108 if (!*xored) return 0;
110 /* Sum the entropy of all values */
111 for(i = 0; i < 256; i++)
112 sum += c->score_tab[histogram[i]];
117 /** Motion estimation function
118 * TODO make better ME decisions
120 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
121 int pstride, int x, int y, int *mx, int *my, int *xored)
123 int dx, dy, txored, tv, bv, bw, bh;
128 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
129 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
132 bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
136 /* Try previous block's MV (if not 0,0) */
138 tv = block_cmp(c, src, sstride, prev + mx0 * c->bypp + my0 * pstride, pstride, bw, bh, &txored);
148 /* Try other MVs from top-to-bottom, left-to-right */
149 for(dy = -c->lrange; dy <= c->urange; dy++){
150 for(dx = -c->lrange; dx <= c->urange; dx++){
151 if(!dx && !dy) continue; // we already tested this block
152 if(dx == mx0 && dy == my0) continue; // this one too
153 tv = block_cmp(c, src, sstride, prev + dx * c->bypp + dy * pstride, pstride, bw, bh, &txored);
166 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
167 const AVFrame *pict, int *got_packet)
169 ZmbvEncContext * const c = avctx->priv_data;
170 const AVFrame * const p = pict;
171 uint8_t *src, *prev, *buf;
175 int work_size = 0, pkt_size;
179 keyframe = !c->curfrm;
181 if(c->curfrm == c->keyint)
184 palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
185 chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
191 for(i = 0; i < 256; i++){
192 AV_WB24(tpal, palptr[i]);
193 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
194 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
195 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
196 c->pal[i * 3 + 0] = tpal[0];
197 c->pal[i * 3 + 1] = tpal[1];
198 c->pal[i * 3 + 2] = tpal[2];
200 memcpy(c->pal2, palptr, 1024);
204 for(i = 0; i < 256; i++){
205 AV_WB24(c->pal+(i*3), palptr[i]);
207 memcpy(c->work_buf, c->pal, 768);
208 memcpy(c->pal2, palptr, 1024);
211 for(i = 0; i < avctx->height; i++){
212 memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
213 src += p->linesize[0];
214 work_size += avctx->width * c->bypp;
217 int x, y, bh2, bw2, xored;
218 uint8_t *tsrc, *tprev;
222 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
223 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
224 mv = c->work_buf + work_size;
225 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
226 work_size += (bw * bh * 2 + 3) & ~3;
227 /* for now just XOR'ing */
228 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
229 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
230 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
231 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
233 tsrc = src + x * c->bypp;
234 tprev = prev + x * c->bypp;
236 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
237 mv[0] = (mx * 2) | !!xored;
239 tprev += mx * c->bypp + my * c->pstride;
241 for(j = 0; j < bh2; j++){
242 for(i = 0; i < bw2 * c->bypp; i++)
243 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
244 tsrc += p->linesize[0];
249 src += p->linesize[0] * ZMBV_BLOCK;
250 prev += c->pstride * ZMBV_BLOCK;
253 /* save the previous frame */
256 for(i = 0; i < avctx->height; i++){
257 memcpy(prev, src, avctx->width * c->bypp);
259 src += p->linesize[0];
263 deflateReset(&c->zstream);
265 c->zstream.next_in = c->work_buf;
266 c->zstream.avail_in = work_size;
267 c->zstream.total_in = 0;
269 c->zstream.next_out = c->comp_buf;
270 c->zstream.avail_out = c->comp_size;
271 c->zstream.total_out = 0;
272 if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
273 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
277 pkt_size = c->zstream.total_out + 1 + 6*keyframe;
278 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
282 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
285 *buf++ = 0; // hi ver
286 *buf++ = 1; // lo ver
288 *buf++ = c->fmt; // format
289 *buf++ = ZMBV_BLOCK; // block width
290 *buf++ = ZMBV_BLOCK; // block height
292 memcpy(buf, c->comp_buf, c->zstream.total_out);
294 pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
300 static av_cold int encode_end(AVCodecContext *avctx)
302 ZmbvEncContext * const c = avctx->priv_data;
304 av_freep(&c->comp_buf);
305 av_freep(&c->work_buf);
307 deflateEnd(&c->zstream);
308 av_freep(&c->prev_buf);
316 static av_cold int encode_init(AVCodecContext *avctx)
318 ZmbvEncContext * const c = avctx->priv_data;
319 int zret; // Zlib return code
322 int prev_size, prev_offset;
324 switch (avctx->pix_fmt) {
325 case AV_PIX_FMT_PAL8:
326 c->fmt = ZMBV_FMT_8BPP;
329 case AV_PIX_FMT_RGB555LE:
330 c->fmt = ZMBV_FMT_15BPP;
333 case AV_PIX_FMT_RGB565LE:
334 c->fmt = ZMBV_FMT_16BPP;
337 #ifdef ZMBV_ENABLE_24BPP
338 case AV_PIX_FMT_BGR24:
339 c->fmt = ZMBV_FMT_24BPP;
342 #endif //ZMBV_ENABLE_24BPP
343 case AV_PIX_FMT_BGR0:
344 c->fmt = ZMBV_FMT_32BPP;
348 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
349 return AVERROR(EINVAL);
352 /* Entropy-based score tables for comparing blocks.
353 * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
354 * Scores are nonnegative, lower is better.
356 for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
357 c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
362 c->keyint = avctx->keyint_min;
364 /* Motion estimation range: maximum distance is -64..63 */
365 c->lrange = c->urange = 8;
366 if(avctx->me_range > 0){
367 c->lrange = FFMIN(avctx->me_range, 64);
368 c->urange = FFMIN(avctx->me_range, 63);
371 if(avctx->compression_level >= 0)
372 lvl = avctx->compression_level;
373 if(lvl < 0 || lvl > 9){
374 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
375 return AVERROR(EINVAL);
378 // Needed if zlib unused or init aborted before deflateInit
379 memset(&c->zstream, 0, sizeof(z_stream));
380 c->comp_size = avctx->width * c->bypp * avctx->height + 1024 +
381 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
382 if (!(c->work_buf = av_malloc(c->comp_size))) {
383 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
384 return AVERROR(ENOMEM);
386 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
387 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
388 ((c->comp_size + 63) >> 6) + 11;
390 /* Allocate compression buffer */
391 if (!(c->comp_buf = av_malloc(c->comp_size))) {
392 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
393 return AVERROR(ENOMEM);
396 /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
397 * - The image should be padded with `lrange` rows before and `urange` rows
399 * - The stride should be padded with `lrange` pixels, then rounded up to a
400 * multiple of 16 bytes.
401 * - The first row should also be padded with `lrange` pixels before, then
402 * aligned up to a multiple of 16 bytes.
404 c->pstride = FFALIGN((avctx->width + c->lrange) * c->bypp, 16);
405 prev_size = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
406 prev_offset = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * c->lrange;
407 if (!(c->prev_buf = av_mallocz(prev_size))) {
408 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
409 return AVERROR(ENOMEM);
411 c->prev = c->prev_buf + prev_offset;
413 c->zstream.zalloc = Z_NULL;
414 c->zstream.zfree = Z_NULL;
415 c->zstream.opaque = Z_NULL;
416 zret = deflateInit(&c->zstream, lvl);
418 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
425 AVCodec ff_zmbv_encoder = {
427 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
428 .type = AVMEDIA_TYPE_VIDEO,
429 .id = AV_CODEC_ID_ZMBV,
430 .priv_data_size = sizeof(ZmbvEncContext),
432 .encode2 = encode_frame,
434 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
437 #ifdef ZMBV_ENABLE_24BPP
439 #endif //ZMBV_ENABLE_24BPP