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"
33 #include "codec_internal.h"
35 #include "zlib_wrapper.h"
39 /* Frame header flags */
40 #define ZMBV_KEYFRAME 1
41 #define ZMBV_DELTAPAL 2
43 /* Motion block width/height (maximum allowed value is 255)
44 * Note: histogram datatype in block_cmp() must be big enough to hold values
45 * up to (4 * ZMBV_BLOCK * ZMBV_BLOCK)
49 /* Keyframe header format values */
65 typedef struct ZmbvEncContext {
66 AVCodecContext *avctx;
69 uint8_t *comp_buf, *work_buf;
71 uint32_t pal2[256]; //for quick comparisons
72 uint8_t *prev, *prev_buf;
80 int score_tab[ZMBV_BLOCK * ZMBV_BLOCK * 4 + 1];
84 /** Block comparing function
85 * XXX should be optimized and moved to DSPContext
87 static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
88 uint8_t *src2, int stride2, int bw, int bh,
93 uint16_t histogram[256] = {0};
94 int bw_bytes = bw * c->bypp;
96 /* Build frequency histogram of byte values for src[] ^ src2[] */
97 for(j = 0; j < bh; j++){
98 for(i = 0; i < bw_bytes; i++){
99 int t = src[i] ^ src2[i];
106 /* If not all the xored values were 0, then the blocks are different */
107 *xored = (histogram[0] < bw_bytes * bh);
109 /* Exit early if blocks are equal */
110 if (!*xored) return 0;
112 /* Sum the entropy of all values */
113 for(i = 0; i < 256; i++)
114 sum += c->score_tab[histogram[i]];
119 /** Motion estimation function
120 * TODO make better ME decisions
122 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
123 int pstride, int x, int y, int *mx, int *my, int *xored)
125 int dx, dy, txored, tv, bv, bw, bh;
130 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
131 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
134 bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
138 /* Try previous block's MV (if not 0,0) */
140 tv = block_cmp(c, src, sstride, prev + mx0 * c->bypp + my0 * pstride, pstride, bw, bh, &txored);
150 /* Try other MVs from top-to-bottom, left-to-right */
151 for(dy = -c->lrange; dy <= c->urange; dy++){
152 for(dx = -c->lrange; dx <= c->urange; dx++){
153 if(!dx && !dy) continue; // we already tested this block
154 if(dx == mx0 && dy == my0) continue; // this one too
155 tv = block_cmp(c, src, sstride, prev + dx * c->bypp + dy * pstride, pstride, bw, bh, &txored);
168 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
169 const AVFrame *pict, int *got_packet)
171 ZmbvEncContext * const c = avctx->priv_data;
172 z_stream *const zstream = &c->zstream.zstream;
173 const AVFrame * const p = pict;
174 uint8_t *src, *prev, *buf;
178 int work_size = 0, pkt_size;
182 keyframe = !c->curfrm;
184 if(c->curfrm == c->keyint)
187 palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
188 chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
194 for(i = 0; i < 256; i++){
195 AV_WB24(tpal, palptr[i]);
196 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
197 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
198 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
199 c->pal[i * 3 + 0] = tpal[0];
200 c->pal[i * 3 + 1] = tpal[1];
201 c->pal[i * 3 + 2] = tpal[2];
203 memcpy(c->pal2, palptr, 1024);
207 for(i = 0; i < 256; i++){
208 AV_WB24(c->pal+(i*3), palptr[i]);
210 memcpy(c->work_buf, c->pal, 768);
211 memcpy(c->pal2, palptr, 1024);
214 for(i = 0; i < avctx->height; i++){
215 memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
216 src += p->linesize[0];
217 work_size += avctx->width * c->bypp;
220 int x, y, bh2, bw2, xored;
221 uint8_t *tsrc, *tprev;
225 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
226 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
227 mv = c->work_buf + work_size;
228 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
229 work_size += (bw * bh * 2 + 3) & ~3;
230 /* for now just XOR'ing */
231 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
232 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
233 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
234 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
236 tsrc = src + x * c->bypp;
237 tprev = prev + x * c->bypp;
239 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
240 mv[0] = (mx * 2) | !!xored;
242 tprev += mx * c->bypp + my * c->pstride;
244 for(j = 0; j < bh2; j++){
245 for(i = 0; i < bw2 * c->bypp; i++)
246 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
247 tsrc += p->linesize[0];
252 src += p->linesize[0] * ZMBV_BLOCK;
253 prev += c->pstride * ZMBV_BLOCK;
256 /* save the previous frame */
259 for(i = 0; i < avctx->height; i++){
260 memcpy(prev, src, avctx->width * c->bypp);
262 src += p->linesize[0];
266 deflateReset(zstream);
268 zstream->next_in = c->work_buf;
269 zstream->avail_in = work_size;
270 zstream->total_in = 0;
272 zstream->next_out = c->comp_buf;
273 zstream->avail_out = c->comp_size;
274 zstream->total_out = 0;
275 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
276 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
280 pkt_size = zstream->total_out + 1 + 6 * keyframe;
281 if ((ret = ff_get_encode_buffer(avctx, pkt, pkt_size, 0)) < 0)
285 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
288 *buf++ = 0; // hi ver
289 *buf++ = 1; // lo ver
291 *buf++ = c->fmt; // format
292 *buf++ = ZMBV_BLOCK; // block width
293 *buf++ = ZMBV_BLOCK; // block height
294 pkt->flags |= AV_PKT_FLAG_KEY;
296 memcpy(buf, c->comp_buf, zstream->total_out);
303 static av_cold int encode_end(AVCodecContext *avctx)
305 ZmbvEncContext * const c = avctx->priv_data;
307 av_freep(&c->comp_buf);
308 av_freep(&c->work_buf);
310 av_freep(&c->prev_buf);
311 ff_deflate_end(&c->zstream);
319 static av_cold int encode_init(AVCodecContext *avctx)
321 ZmbvEncContext * const c = avctx->priv_data;
324 int prev_size, prev_offset;
326 switch (avctx->pix_fmt) {
327 case AV_PIX_FMT_PAL8:
328 c->fmt = ZMBV_FMT_8BPP;
331 case AV_PIX_FMT_RGB555LE:
332 c->fmt = ZMBV_FMT_15BPP;
335 case AV_PIX_FMT_RGB565LE:
336 c->fmt = ZMBV_FMT_16BPP;
339 #ifdef ZMBV_ENABLE_24BPP
340 case AV_PIX_FMT_BGR24:
341 c->fmt = ZMBV_FMT_24BPP;
344 #endif //ZMBV_ENABLE_24BPP
345 case AV_PIX_FMT_BGR0:
346 c->fmt = ZMBV_FMT_32BPP;
351 /* Entropy-based score tables for comparing blocks.
352 * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
353 * Scores are nonnegative, lower is better.
355 for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
356 c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
361 c->keyint = avctx->keyint_min;
363 /* Motion estimation range: maximum distance is -64..63 */
364 c->lrange = c->urange = 8;
365 if(avctx->me_range > 0){
366 c->lrange = FFMIN(avctx->me_range, 64);
367 c->urange = FFMIN(avctx->me_range, 63);
370 if(avctx->compression_level >= 0)
371 lvl = avctx->compression_level;
372 if(lvl < 0 || lvl > 9){
373 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
374 return AVERROR(EINVAL);
377 c->comp_size = avctx->width * c->bypp * avctx->height + 1024 +
378 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
379 if (!(c->work_buf = av_malloc(c->comp_size))) {
380 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
381 return AVERROR(ENOMEM);
383 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
384 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
385 ((c->comp_size + 63) >> 6) + 11;
387 /* Allocate compression buffer */
388 if (!(c->comp_buf = av_malloc(c->comp_size))) {
389 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
390 return AVERROR(ENOMEM);
393 /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
394 * - The image should be padded with `lrange` rows before and `urange` rows
396 * - The stride should be padded with `lrange` pixels, then rounded up to a
397 * multiple of 16 bytes.
398 * - The first row should also be padded with `lrange` pixels before, then
399 * aligned up to a multiple of 16 bytes.
401 c->pstride = FFALIGN((avctx->width + c->lrange) * c->bypp, 16);
402 prev_size = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
403 prev_offset = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * c->lrange;
404 if (!(c->prev_buf = av_mallocz(prev_size))) {
405 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
406 return AVERROR(ENOMEM);
408 c->prev = c->prev_buf + prev_offset;
410 return ff_deflate_init(&c->zstream, lvl, avctx);
413 const FFCodec ff_zmbv_encoder = {
415 .p.long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
416 .p.type = AVMEDIA_TYPE_VIDEO,
417 .p.id = AV_CODEC_ID_ZMBV,
418 .p.capabilities = AV_CODEC_CAP_DR1,
419 .priv_data_size = sizeof(ZmbvEncContext),
421 FF_CODEC_ENCODE_CB(encode_frame),
423 .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
426 #ifdef ZMBV_ENABLE_24BPP
428 #endif //ZMBV_ENABLE_24BPP
431 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,