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