1/*
2 * Zip Motion Blocks Video (ZMBV) encoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file libavcodec/zmbvenc.c
24 * Zip Motion Blocks Video encoder
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29
30#include "libavutil/intreadwrite.h"
31#include "avcodec.h"
32
33#include <zlib.h>
34
35#define ZMBV_KEYFRAME 1
36#define ZMBV_DELTAPAL 2
37
38#define ZMBV_BLOCK 16
39
40/**
41 * Encoder context
42 */
43typedef struct ZmbvEncContext {
44    AVCodecContext *avctx;
45    AVFrame pic;
46
47    int range;
48    uint8_t *comp_buf, *work_buf;
49    uint8_t pal[768];
50    uint32_t pal2[256]; //for quick comparisons
51    uint8_t *prev;
52    int pstride;
53    int comp_size;
54    int keyint, curfrm;
55    z_stream zstream;
56} ZmbvEncContext;
57
58static int score_tab[256];
59
60/** Block comparing function
61 * XXX should be optimized and moved to DSPContext
62 * TODO handle out of edge ME
63 */
64static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
65{
66    int sum = 0;
67    int i, j;
68    uint8_t histogram[256]={0};
69
70    for(j = 0; j < bh; j++){
71        for(i = 0; i < bw; i++)
72            histogram[src[i] ^ src2[i]]++;
73        src += stride;
74        src2 += stride2;
75    }
76
77    for(i=1; i<256; i++)
78        sum+= score_tab[histogram[i]];
79
80    return sum;
81}
82
83/** Motion estimation function
84 * TODO make better ME decisions
85 */
86static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
87                    int x, int y, int *mx, int *my)
88{
89    int dx, dy, tx, ty, tv, bv, bw, bh;
90
91    *mx = *my = 0;
92    bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
93    bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
94    bv = block_cmp(src, sstride, prev, pstride, bw, bh);
95    if(!bv) return 0;
96    for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
97        for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
98            if(tx == x && ty == y) continue; // we already tested this block
99            dx = tx - x;
100            dy = ty - y;
101            tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh);
102            if(tv < bv){
103                 bv = tv;
104                 *mx = dx;
105                 *my = dy;
106                 if(!bv) return 0;
107             }
108         }
109    }
110    return bv;
111}
112
113static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
114{
115    ZmbvEncContext * const c = avctx->priv_data;
116    AVFrame *pict = data;
117    AVFrame * const p = &c->pic;
118    uint8_t *src, *prev;
119    uint32_t *palptr;
120    int zret = Z_OK;
121    int len = 0;
122    int keyframe, chpal;
123    int fl;
124    int work_size = 0;
125    int bw, bh;
126    int i, j;
127
128    keyframe = !c->curfrm;
129    c->curfrm++;
130    if(c->curfrm == c->keyint)
131        c->curfrm = 0;
132    *p = *pict;
133    p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
134    p->key_frame= keyframe;
135    chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
136
137    fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
138    *buf++ = fl; len++;
139    if(keyframe){
140        deflateReset(&c->zstream);
141        *buf++ = 0; len++; // hi ver
142        *buf++ = 1; len++; // lo ver
143        *buf++ = 1; len++; // comp
144        *buf++ = 4; len++; // format - 8bpp
145        *buf++ = ZMBV_BLOCK; len++; // block width
146        *buf++ = ZMBV_BLOCK; len++; // block height
147    }
148    palptr = (uint32_t*)p->data[1];
149    src = p->data[0];
150    prev = c->prev;
151    if(chpal){
152        uint8_t tpal[3];
153        for(i = 0; i < 256; i++){
154            AV_WB24(tpal, palptr[i]);
155            c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
156            c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
157            c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
158            c->pal[i * 3 + 0] = tpal[0];
159            c->pal[i * 3 + 1] = tpal[1];
160            c->pal[i * 3 + 2] = tpal[2];
161        }
162        memcpy(c->pal2, p->data[1], 1024);
163    }
164    if(keyframe){
165        for(i = 0; i < 256; i++){
166            AV_WB24(c->pal+(i*3), palptr[i]);
167        }
168        memcpy(c->work_buf, c->pal, 768);
169        memcpy(c->pal2, p->data[1], 1024);
170        work_size = 768;
171        for(i = 0; i < avctx->height; i++){
172            memcpy(c->work_buf + work_size, src, avctx->width);
173            src += p->linesize[0];
174            work_size += avctx->width;
175        }
176    }else{
177        int x, y, bh2, bw2;
178        uint8_t *tsrc, *tprev;
179        uint8_t *mv;
180        int mx, my, bv;
181
182        bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
183        bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
184        mv = c->work_buf + work_size;
185        memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
186        work_size += (bw * bh * 2 + 3) & ~3;
187        /* for now just XOR'ing */
188        for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
189            bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
190            for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
191                bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
192
193                tsrc = src + x;
194                tprev = prev + x;
195
196                bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
197                mv[0] = (mx << 1) | !!bv;
198                mv[1] = my << 1;
199                tprev += mx + my * c->pstride;
200                if(bv){
201                    for(j = 0; j < bh2; j++){
202                        for(i = 0; i < bw2; i++)
203                            c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
204                        tsrc += p->linesize[0];
205                        tprev += c->pstride;
206                    }
207                }
208            }
209            src += p->linesize[0] * ZMBV_BLOCK;
210            prev += c->pstride * ZMBV_BLOCK;
211        }
212    }
213    /* save the previous frame */
214    src = p->data[0];
215    prev = c->prev;
216    for(i = 0; i < avctx->height; i++){
217        memcpy(prev, src, avctx->width);
218        prev += c->pstride;
219        src += p->linesize[0];
220    }
221
222    c->zstream.next_in = c->work_buf;
223    c->zstream.avail_in = work_size;
224    c->zstream.total_in = 0;
225
226    c->zstream.next_out = c->comp_buf;
227    c->zstream.avail_out = c->comp_size;
228    c->zstream.total_out = 0;
229    if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
230        av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
231        return -1;
232    }
233
234    memcpy(buf, c->comp_buf, c->zstream.total_out);
235    return len + c->zstream.total_out;
236}
237
238
239/**
240 * Init zmbv encoder
241 */
242static av_cold int encode_init(AVCodecContext *avctx)
243{
244    ZmbvEncContext * const c = avctx->priv_data;
245    int zret; // Zlib return code
246    int i;
247    int lvl = 9;
248
249    for(i=1; i<256; i++)
250        score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
251
252    c->avctx = avctx;
253
254    c->pic.data[0] = NULL;
255    c->curfrm = 0;
256    c->keyint = avctx->keyint_min;
257    c->range = 8;
258    if(avctx->me_range > 0)
259        c->range = FFMIN(avctx->me_range, 127);
260
261    if(avctx->compression_level >= 0)
262        lvl = avctx->compression_level;
263    if(lvl < 0 || lvl > 9){
264        av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
265        return -1;
266    }
267
268    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
269        return -1;
270    }
271
272    // Needed if zlib unused or init aborted before deflateInit
273    memset(&(c->zstream), 0, sizeof(z_stream));
274    c->comp_size = avctx->width * avctx->height + 1024 +
275        ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
276    if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
277        av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
278        return -1;
279    }
280    /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
281    c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
282                           ((c->comp_size + 63) >> 6) + 11;
283
284    /* Allocate compression buffer */
285    if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
286        av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
287        return -1;
288    }
289    c->pstride = (avctx->width + 15) & ~15;
290    if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
291        av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
292        return -1;
293    }
294
295    c->zstream.zalloc = Z_NULL;
296    c->zstream.zfree = Z_NULL;
297    c->zstream.opaque = Z_NULL;
298    zret = deflateInit(&(c->zstream), lvl);
299    if (zret != Z_OK) {
300        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
301        return -1;
302    }
303
304    avctx->coded_frame = (AVFrame*)&c->pic;
305
306    return 0;
307}
308
309
310
311/**
312 * Uninit zmbv encoder
313 */
314static av_cold int encode_end(AVCodecContext *avctx)
315{
316    ZmbvEncContext * const c = avctx->priv_data;
317
318    av_freep(&c->comp_buf);
319    av_freep(&c->work_buf);
320
321    deflateEnd(&(c->zstream));
322    av_freep(&c->prev);
323
324    return 0;
325}
326
327AVCodec zmbv_encoder = {
328    "zmbv",
329    CODEC_TYPE_VIDEO,
330    CODEC_ID_ZMBV,
331    sizeof(ZmbvEncContext),
332    encode_init,
333    encode_frame,
334    encode_end,
335    .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
336    .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
337};
338