]> git.sesse.net Git - ffmpeg/blob - libavcodec/zmbvenc.c
remove useless #ifdef CONFIG_ZLIB
[ffmpeg] / 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;
84
85     *mx = *my = 0;
86     bv = block_cmp(src, sstride, prev, pstride, ZMBV_BLOCK, ZMBV_BLOCK);
87     if(!bv) return 0;
88     for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - ZMBV_BLOCK); ty++){
89         for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - ZMBV_BLOCK); tx++){
90             if(tx == x && ty == y) continue; // we already tested this block
91             dx = tx - x;
92             dy = ty - y;
93             tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, ZMBV_BLOCK, ZMBV_BLOCK);
94             if(tv < bv){
95                  bv = tv;
96                  *mx = dx;
97                  *my = dy;
98                  if(!bv) return 0;
99              }
100          }
101     }
102     return bv;
103 }
104
105 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
106 {
107     ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
108     AVFrame *pict = data;
109     AVFrame * const p = &c->pic;
110     uint8_t *src, *prev;
111     uint32_t *palptr;
112     int zret = Z_OK;
113     int len = 0;
114     int keyframe, chpal;
115     int fl;
116     int work_size = 0;
117     int bw, bh;
118     int i, j;
119
120     keyframe = !c->curfrm;
121     c->curfrm++;
122     if(c->curfrm == c->keyint)
123         c->curfrm = 0;
124     *p = *pict;
125     p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
126     p->key_frame= keyframe;
127     chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
128
129     fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
130     *buf++ = fl; len++;
131     if(keyframe){
132         deflateReset(&c->zstream);
133         *buf++ = 0; len++; // hi ver
134         *buf++ = 1; len++; // lo ver
135         *buf++ = 1; len++; // comp
136         *buf++ = 4; len++; // format - 8bpp
137         *buf++ = ZMBV_BLOCK; len++; // block width
138         *buf++ = ZMBV_BLOCK; len++; // block height
139     }
140     palptr = (uint32_t*)p->data[1];
141     src = p->data[0];
142     prev = c->prev;
143     if(chpal){
144         uint8_t tpal[3];
145         for(i = 0; i < 256; i++){
146             tpal[0] = palptr[i] >> 16;
147             tpal[1] = palptr[i] >>  8;
148             tpal[2] = palptr[i];
149             c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
150             c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
151             c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
152             c->pal[i * 3 + 0] = tpal[0];
153             c->pal[i * 3 + 1] = tpal[1];
154             c->pal[i * 3 + 2] = tpal[2];
155         }
156         memcpy(c->pal2, p->data[1], 1024);
157     }
158     if(keyframe){
159         for(i = 0; i < 256; i++){
160             c->pal[i*3 + 0] = palptr[i] >> 16;
161             c->pal[i*3 + 1] = palptr[i] >>  8;
162             c->pal[i*3 + 2] = palptr[i];
163         }
164         memcpy(c->work_buf, c->pal, 768);
165         memcpy(c->pal2, p->data[1], 1024);
166         work_size = 768;
167         for(i = 0; i < avctx->height; i++){
168             memcpy(c->work_buf + work_size, src, avctx->width);
169             src += p->linesize[0];
170             work_size += avctx->width;
171         }
172     }else{
173         int x, y, bh2, bw2;
174         uint8_t *tsrc, *tprev;
175         uint8_t *mv;
176         int mx, my, bv;
177
178         bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
179         bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
180         mv = c->work_buf + work_size;
181         memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
182         work_size += (bw * bh * 2 + 3) & ~3;
183         /* for now just XOR'ing */
184         for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
185             bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
186             for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
187                 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
188
189                 tsrc = src + x;
190                 tprev = prev + x;
191
192                 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
193                 mv[0] = (mx << 1) | !!bv;
194                 mv[1] = my << 1;
195                 tprev += mx + my * c->pstride;
196                 if(bv){
197                     for(j = 0; j < bh2; j++){
198                         for(i = 0; i < bw2; i++)
199                             c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
200                         tsrc += p->linesize[0];
201                         tprev += c->pstride;
202                     }
203                 }
204             }
205             src += p->linesize[0] * ZMBV_BLOCK;
206             prev += c->pstride * ZMBV_BLOCK;
207         }
208     }
209     /* save the previous frame */
210     src = p->data[0];
211     prev = c->prev;
212     for(i = 0; i < avctx->height; i++){
213         memcpy(prev, src, avctx->width);
214         prev += c->pstride;
215         src += p->linesize[0];
216     }
217
218     c->zstream.next_in = c->work_buf;
219     c->zstream.avail_in = work_size;
220     c->zstream.total_in = 0;
221
222     c->zstream.next_out = c->comp_buf;
223     c->zstream.avail_out = c->comp_size;
224     c->zstream.total_out = 0;
225     if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
226         av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
227         return -1;
228     }
229
230     memcpy(buf, c->comp_buf, c->zstream.total_out);
231     return len + c->zstream.total_out;
232 }
233
234
235 /**
236  * Init zmbv encoder
237  */
238 static int encode_init(AVCodecContext *avctx)
239 {
240     ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
241     int zret; // Zlib return code
242     int lvl = 9;
243
244     c->avctx = avctx;
245
246     c->pic.data[0] = NULL;
247     c->curfrm = 0;
248     c->keyint = avctx->keyint_min;
249     c->range = 8;
250     if(avctx->me_range > 0)
251         c->range = FFMIN(avctx->me_range, 127);
252
253     if(avctx->compression_level >= 0)
254         lvl = avctx->compression_level;
255     if(lvl < 0 || lvl > 9){
256         av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
257         return -1;
258     }
259
260     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
261         return -1;
262     }
263
264     // Needed if zlib unused or init aborted before deflateInit
265     memset(&(c->zstream), 0, sizeof(z_stream));
266     c->comp_size = avctx->width * avctx->height + 1024 +
267         ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
268     if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
269         av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
270         return -1;
271     }
272     /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
273     c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
274                            ((c->comp_size + 63) >> 6) + 11;
275
276     /* Allocate compression buffer */
277     if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
278         av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
279         return -1;
280     }
281     c->pstride = (avctx->width + 15) & ~15;
282     if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
283         av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
284         return -1;
285     }
286
287     c->zstream.zalloc = Z_NULL;
288     c->zstream.zfree = Z_NULL;
289     c->zstream.opaque = Z_NULL;
290     zret = deflateInit(&(c->zstream), lvl);
291     if (zret != Z_OK) {
292         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
293         return -1;
294     }
295
296     return 0;
297 }
298
299
300
301 /**
302  * Uninit zmbv encoder
303  */
304 static int encode_end(AVCodecContext *avctx)
305 {
306     ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
307
308     av_freep(&c->comp_buf);
309     av_freep(&c->work_buf);
310
311     deflateEnd(&(c->zstream));
312     av_freep(&c->prev);
313
314     return 0;
315 }
316
317 AVCodec zmbv_encoder = {
318     "zmbv",
319     CODEC_TYPE_VIDEO,
320     CODEC_ID_ZMBV,
321     sizeof(ZmbvEncContext),
322     encode_init,
323     encode_frame,
324     encode_end,
325     .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},
326 };