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)
183 #if FF_API_CODED_FRAME
184 FF_DISABLE_DEPRECATION_WARNINGS
185 avctx->coded_frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
186 avctx->coded_frame->key_frame = keyframe;
187 FF_ENABLE_DEPRECATION_WARNINGS
190 palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
191 chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
197 for(i = 0; i < 256; i++){
198 AV_WB24(tpal, palptr[i]);
199 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
200 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
201 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
202 c->pal[i * 3 + 0] = tpal[0];
203 c->pal[i * 3 + 1] = tpal[1];
204 c->pal[i * 3 + 2] = tpal[2];
206 memcpy(c->pal2, palptr, 1024);
210 for(i = 0; i < 256; i++){
211 AV_WB24(c->pal+(i*3), palptr[i]);
213 memcpy(c->work_buf, c->pal, 768);
214 memcpy(c->pal2, palptr, 1024);
217 for(i = 0; i < avctx->height; i++){
218 memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
219 src += p->linesize[0];
220 work_size += avctx->width * c->bypp;
223 int x, y, bh2, bw2, xored;
224 uint8_t *tsrc, *tprev;
228 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
229 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
230 mv = c->work_buf + work_size;
231 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
232 work_size += (bw * bh * 2 + 3) & ~3;
233 /* for now just XOR'ing */
234 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
235 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
236 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
237 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
239 tsrc = src + x * c->bypp;
240 tprev = prev + x * c->bypp;
242 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
243 mv[0] = (mx * 2) | !!xored;
245 tprev += mx * c->bypp + my * c->pstride;
247 for(j = 0; j < bh2; j++){
248 for(i = 0; i < bw2 * c->bypp; i++)
249 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
250 tsrc += p->linesize[0];
255 src += p->linesize[0] * ZMBV_BLOCK;
256 prev += c->pstride * ZMBV_BLOCK;
259 /* save the previous frame */
262 for(i = 0; i < avctx->height; i++){
263 memcpy(prev, src, avctx->width * c->bypp);
265 src += p->linesize[0];
269 deflateReset(&c->zstream);
271 c->zstream.next_in = c->work_buf;
272 c->zstream.avail_in = work_size;
273 c->zstream.total_in = 0;
275 c->zstream.next_out = c->comp_buf;
276 c->zstream.avail_out = c->comp_size;
277 c->zstream.total_out = 0;
278 if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
279 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
283 pkt_size = c->zstream.total_out + 1 + 6*keyframe;
284 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
288 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
291 *buf++ = 0; // hi ver
292 *buf++ = 1; // lo ver
294 *buf++ = c->fmt; // format
295 *buf++ = ZMBV_BLOCK; // block width
296 *buf++ = ZMBV_BLOCK; // block height
298 memcpy(buf, c->comp_buf, c->zstream.total_out);
300 pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
306 static av_cold 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);
314 av_freep(&c->prev_buf);
322 static av_cold int encode_init(AVCodecContext *avctx)
324 ZmbvEncContext * const c = avctx->priv_data;
325 int zret; // Zlib return code
328 int prev_size, prev_offset;
330 switch (avctx->pix_fmt) {
331 case AV_PIX_FMT_PAL8:
332 c->fmt = ZMBV_FMT_8BPP;
335 case AV_PIX_FMT_RGB555LE:
336 c->fmt = ZMBV_FMT_15BPP;
339 case AV_PIX_FMT_RGB565LE:
340 c->fmt = ZMBV_FMT_16BPP;
343 #ifdef ZMBV_ENABLE_24BPP
344 case AV_PIX_FMT_BGR24:
345 c->fmt = ZMBV_FMT_24BPP;
348 #endif //ZMBV_ENABLE_24BPP
349 case AV_PIX_FMT_BGR0:
350 c->fmt = ZMBV_FMT_32BPP;
354 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
355 return AVERROR(EINVAL);
358 /* Entropy-based score tables for comparing blocks.
359 * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
360 * Scores are nonnegative, lower is better.
362 for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
363 c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
368 c->keyint = avctx->keyint_min;
370 /* Motion estimation range: maximum distance is -64..63 */
371 c->lrange = c->urange = 8;
372 if(avctx->me_range > 0){
373 c->lrange = FFMIN(avctx->me_range, 64);
374 c->urange = FFMIN(avctx->me_range, 63);
377 if(avctx->compression_level >= 0)
378 lvl = avctx->compression_level;
379 if(lvl < 0 || lvl > 9){
380 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
381 return AVERROR(EINVAL);
384 // Needed if zlib unused or init aborted before deflateInit
385 memset(&c->zstream, 0, sizeof(z_stream));
386 c->comp_size = avctx->width * c->bypp * avctx->height + 1024 +
387 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
388 if (!(c->work_buf = av_malloc(c->comp_size))) {
389 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
390 return AVERROR(ENOMEM);
392 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
393 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
394 ((c->comp_size + 63) >> 6) + 11;
396 /* Allocate compression buffer */
397 if (!(c->comp_buf = av_malloc(c->comp_size))) {
398 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
399 return AVERROR(ENOMEM);
402 /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
403 * - The image should be padded with `lrange` rows before and `urange` rows
405 * - The stride should be padded with `lrange` pixels, then rounded up to a
406 * multiple of 16 bytes.
407 * - The first row should also be padded with `lrange` pixels before, then
408 * aligned up to a multiple of 16 bytes.
410 c->pstride = FFALIGN((avctx->width + c->lrange) * c->bypp, 16);
411 prev_size = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
412 prev_offset = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * c->lrange;
413 if (!(c->prev_buf = av_mallocz(prev_size))) {
414 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
415 return AVERROR(ENOMEM);
417 c->prev = c->prev_buf + prev_offset;
419 c->zstream.zalloc = Z_NULL;
420 c->zstream.zfree = Z_NULL;
421 c->zstream.opaque = Z_NULL;
422 zret = deflateInit(&c->zstream, lvl);
424 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
431 AVCodec ff_zmbv_encoder = {
433 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
434 .type = AVMEDIA_TYPE_VIDEO,
435 .id = AV_CODEC_ID_ZMBV,
436 .priv_data_size = sizeof(ZmbvEncContext),
438 .encode2 = encode_frame,
440 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
443 #ifdef ZMBV_ENABLE_24BPP
445 #endif //ZMBV_ENABLE_24BPP