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 #define ZMBV_KEYFRAME 1
38 #define ZMBV_DELTAPAL 2
45 typedef struct ZmbvEncContext {
46 AVCodecContext *avctx;
49 uint8_t *comp_buf, *work_buf;
51 uint32_t pal2[256]; //for quick comparisons
62 /** Block comparing function
63 * XXX should be optimized and moved to DSPContext
64 * TODO handle out of edge ME
66 static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
67 uint8_t *src2, int stride2, int bw, int bh,
72 uint8_t histogram[256] = {0};
75 for(j = 0; j < bh; j++){
76 for(i = 0; i < bw; i++){
77 int t = src[i] ^ src2[i];
85 for(i = 1; i < 256; i++)
86 sum += c->score_tab[histogram[i]];
91 /** Motion estimation function
92 * TODO make better ME decisions
94 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
95 int pstride, int x, int y, int *mx, int *my, int *xored)
97 int dx, dy, tx, ty, tv, bv, bw, bh;
100 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
101 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
102 bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
104 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
105 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
106 if(tx == x && ty == y) continue; // we already tested this block
109 tv = block_cmp(c, src, sstride, prev + dx + dy * pstride, pstride, bw, bh, xored);
121 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
122 const AVFrame *pict, int *got_packet)
124 ZmbvEncContext * const c = avctx->priv_data;
125 const AVFrame * const p = pict;
126 uint8_t *src, *prev, *buf;
130 int work_size = 0, pkt_size;
134 keyframe = !c->curfrm;
136 if(c->curfrm == c->keyint)
138 #if FF_API_CODED_FRAME
139 FF_DISABLE_DEPRECATION_WARNINGS
140 avctx->coded_frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
141 avctx->coded_frame->key_frame = keyframe;
142 FF_ENABLE_DEPRECATION_WARNINGS
144 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
146 palptr = (uint32_t*)p->data[1];
151 for(i = 0; i < 256; i++){
152 AV_WB24(tpal, palptr[i]);
153 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
154 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
155 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
156 c->pal[i * 3 + 0] = tpal[0];
157 c->pal[i * 3 + 1] = tpal[1];
158 c->pal[i * 3 + 2] = tpal[2];
160 memcpy(c->pal2, p->data[1], 1024);
163 for(i = 0; i < 256; i++){
164 AV_WB24(c->pal+(i*3), 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;
175 int x, y, bh2, bw2, xored;
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 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
195 mv[0] = (mx << 1) | !!xored;
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];
221 deflateReset(&c->zstream);
223 c->zstream.next_in = c->work_buf;
224 c->zstream.avail_in = work_size;
225 c->zstream.total_in = 0;
227 c->zstream.next_out = c->comp_buf;
228 c->zstream.avail_out = c->comp_size;
229 c->zstream.total_out = 0;
230 if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
231 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
235 pkt_size = c->zstream.total_out + 1 + 6*keyframe;
236 if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
240 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
243 *buf++ = 0; // hi ver
244 *buf++ = 1; // lo ver
246 *buf++ = 4; // format - 8bpp
247 *buf++ = ZMBV_BLOCK; // block width
248 *buf++ = ZMBV_BLOCK; // block height
250 memcpy(buf, c->comp_buf, c->zstream.total_out);
252 pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
258 static av_cold int encode_end(AVCodecContext *avctx)
260 ZmbvEncContext * const c = avctx->priv_data;
262 av_freep(&c->comp_buf);
263 av_freep(&c->work_buf);
265 deflateEnd(&c->zstream);
274 static av_cold int encode_init(AVCodecContext *avctx)
276 ZmbvEncContext * const c = avctx->priv_data;
277 int zret; // Zlib return code
282 c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK)) * 256;
287 c->keyint = avctx->keyint_min;
289 if(avctx->me_range > 0)
290 c->range = FFMIN(avctx->me_range, 127);
292 if(avctx->compression_level >= 0)
293 lvl = avctx->compression_level;
294 if(lvl < 0 || lvl > 9){
295 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
296 return AVERROR(EINVAL);
299 // Needed if zlib unused or init aborted before deflateInit
300 memset(&c->zstream, 0, sizeof(z_stream));
301 c->comp_size = avctx->width * avctx->height + 1024 +
302 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
303 if (!(c->work_buf = av_malloc(c->comp_size))) {
304 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
305 return AVERROR(ENOMEM);
307 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
308 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
309 ((c->comp_size + 63) >> 6) + 11;
311 /* Allocate compression buffer */
312 if (!(c->comp_buf = av_malloc(c->comp_size))) {
313 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
314 return AVERROR(ENOMEM);
316 c->pstride = FFALIGN(avctx->width, 16);
317 if (!(c->prev = av_malloc(c->pstride * avctx->height))) {
318 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
319 return AVERROR(ENOMEM);
322 c->zstream.zalloc = Z_NULL;
323 c->zstream.zfree = Z_NULL;
324 c->zstream.opaque = Z_NULL;
325 zret = deflateInit(&c->zstream, lvl);
327 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
334 AVCodec ff_zmbv_encoder = {
336 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
337 .type = AVMEDIA_TYPE_VIDEO,
338 .id = AV_CODEC_ID_ZMBV,
339 .priv_data_size = sizeof(ZmbvEncContext),
341 .encode2 = encode_frame,
343 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE },