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