]> git.sesse.net Git - ffmpeg/blob - libavcodec/zmbvenc.c
Merge commit '1ff6cb2ca6652e7d2a929afd33d8ed6268c45568'
[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
48     int lrange, urange;
49     uint8_t *comp_buf, *work_buf;
50     uint8_t pal[768];
51     uint32_t pal2[256]; //for quick comparisons
52     uint8_t *prev, *prev_buf;
53     int pstride;
54     int comp_size;
55     int keyint, curfrm;
56     z_stream zstream;
57
58     int score_tab[ZMBV_BLOCK * ZMBV_BLOCK + 1];
59 } ZmbvEncContext;
60
61
62 /** Block comparing function
63  * XXX should be optimized and moved to DSPContext
64  */
65 static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
66                             uint8_t *src2, int stride2, int bw, int bh,
67                             int *xored)
68 {
69     int sum = 0;
70     int i, j;
71     uint16_t histogram[256] = {0};
72
73     /* Build frequency histogram of byte values for src[] ^ src2[] */
74     for(j = 0; j < bh; j++){
75         for(i = 0; i < bw; i++){
76             int t = src[i] ^ src2[i];
77             histogram[t]++;
78         }
79         src += stride;
80         src2 += stride2;
81     }
82
83     /* If not all the xored values were 0, then the blocks are different */
84     *xored = (histogram[0] < bw * bh);
85
86     /* Exit early if blocks are equal */
87     if (!*xored) return 0;
88
89     /* Sum the entropy of all values */
90     for(i = 0; i < 256; i++)
91         sum += c->score_tab[histogram[i]];
92
93     return sum;
94 }
95
96 /** Motion estimation function
97  * TODO make better ME decisions
98  */
99 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
100                    int pstride, int x, int y, int *mx, int *my, int *xored)
101 {
102     int dx, dy, txored, tv, bv, bw, bh;
103     int mx0, my0;
104
105     mx0 = *mx;
106     my0 = *my;
107     bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
108     bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
109
110     /* Try (0,0) */
111     bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
112     *mx = *my = 0;
113     if(!bv) return 0;
114
115     /* Try previous block's MV (if not 0,0) */
116     if (mx0 || my0){
117         tv = block_cmp(c, src, sstride, prev + mx0 + my0 * pstride, pstride, bw, bh, &txored);
118         if(tv < bv){
119             bv = tv;
120             *mx = mx0;
121             *my = my0;
122             *xored = txored;
123             if(!bv) return 0;
124         }
125     }
126
127     /* Try other MVs from top-to-bottom, left-to-right */
128     for(dy = -c->lrange; dy <= c->urange; dy++){
129         for(dx = -c->lrange; dx <= c->urange; dx++){
130             if(!dx && !dy) continue; // we already tested this block
131             if(dx == mx0 && dy == my0) continue; // this one too
132             tv = block_cmp(c, src, sstride, prev + dx + dy * pstride, pstride, bw, bh, &txored);
133             if(tv < bv){
134                  bv = tv;
135                  *mx = dx;
136                  *my = dy;
137                  *xored = txored;
138                  if(!bv) return 0;
139              }
140          }
141     }
142     return bv;
143 }
144
145 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
146                         const AVFrame *pict, int *got_packet)
147 {
148     ZmbvEncContext * const c = avctx->priv_data;
149     const AVFrame * const p = pict;
150     uint8_t *src, *prev, *buf;
151     uint32_t *palptr;
152     int keyframe, chpal;
153     int fl;
154     int work_size = 0, pkt_size;
155     int bw, bh;
156     int i, j, ret;
157
158     keyframe = !c->curfrm;
159     c->curfrm++;
160     if(c->curfrm == c->keyint)
161         c->curfrm = 0;
162 #if FF_API_CODED_FRAME
163 FF_DISABLE_DEPRECATION_WARNINGS
164     avctx->coded_frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
165     avctx->coded_frame->key_frame = keyframe;
166 FF_ENABLE_DEPRECATION_WARNINGS
167 #endif
168     chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
169
170     palptr = (uint32_t*)p->data[1];
171     src = p->data[0];
172     prev = c->prev;
173     if(chpal){
174         uint8_t tpal[3];
175         for(i = 0; i < 256; i++){
176             AV_WB24(tpal, palptr[i]);
177             c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
178             c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
179             c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
180             c->pal[i * 3 + 0] = tpal[0];
181             c->pal[i * 3 + 1] = tpal[1];
182             c->pal[i * 3 + 2] = tpal[2];
183         }
184         memcpy(c->pal2, p->data[1], 1024);
185     }
186     if(keyframe){
187         for(i = 0; i < 256; i++){
188             AV_WB24(c->pal+(i*3), palptr[i]);
189         }
190         memcpy(c->work_buf, c->pal, 768);
191         memcpy(c->pal2, p->data[1], 1024);
192         work_size = 768;
193         for(i = 0; i < avctx->height; i++){
194             memcpy(c->work_buf + work_size, src, avctx->width);
195             src += p->linesize[0];
196             work_size += avctx->width;
197         }
198     }else{
199         int x, y, bh2, bw2, xored;
200         uint8_t *tsrc, *tprev;
201         uint8_t *mv;
202         int mx = 0, my = 0;
203
204         bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
205         bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
206         mv = c->work_buf + work_size;
207         memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
208         work_size += (bw * bh * 2 + 3) & ~3;
209         /* for now just XOR'ing */
210         for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
211             bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
212             for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
213                 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
214
215                 tsrc = src + x;
216                 tprev = prev + x;
217
218                 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
219                 mv[0] = (mx << 1) | !!xored;
220                 mv[1] = my << 1;
221                 tprev += mx + my * c->pstride;
222                 if(xored){
223                     for(j = 0; j < bh2; j++){
224                         for(i = 0; i < bw2; i++)
225                             c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
226                         tsrc += p->linesize[0];
227                         tprev += c->pstride;
228                     }
229                 }
230             }
231             src += p->linesize[0] * ZMBV_BLOCK;
232             prev += c->pstride * ZMBV_BLOCK;
233         }
234     }
235     /* save the previous frame */
236     src = p->data[0];
237     prev = c->prev;
238     for(i = 0; i < avctx->height; i++){
239         memcpy(prev, src, avctx->width);
240         prev += c->pstride;
241         src += p->linesize[0];
242     }
243
244     if (keyframe)
245         deflateReset(&c->zstream);
246
247     c->zstream.next_in = c->work_buf;
248     c->zstream.avail_in = work_size;
249     c->zstream.total_in = 0;
250
251     c->zstream.next_out = c->comp_buf;
252     c->zstream.avail_out = c->comp_size;
253     c->zstream.total_out = 0;
254     if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
255         av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
256         return -1;
257     }
258
259     pkt_size = c->zstream.total_out + 1 + 6*keyframe;
260     if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
261         return ret;
262     buf = pkt->data;
263
264     fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
265     *buf++ = fl;
266     if (keyframe) {
267         *buf++ = 0; // hi ver
268         *buf++ = 1; // lo ver
269         *buf++ = 1; // comp
270         *buf++ = 4; // format - 8bpp
271         *buf++ = ZMBV_BLOCK; // block width
272         *buf++ = ZMBV_BLOCK; // block height
273     }
274     memcpy(buf, c->comp_buf, c->zstream.total_out);
275
276     pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
277     *got_packet = 1;
278
279     return 0;
280 }
281
282 static av_cold int encode_end(AVCodecContext *avctx)
283 {
284     ZmbvEncContext * const c = avctx->priv_data;
285
286     av_freep(&c->comp_buf);
287     av_freep(&c->work_buf);
288
289     deflateEnd(&c->zstream);
290     av_freep(&c->prev_buf);
291
292     return 0;
293 }
294
295 /**
296  * Init zmbv encoder
297  */
298 static av_cold int encode_init(AVCodecContext *avctx)
299 {
300     ZmbvEncContext * const c = avctx->priv_data;
301     int zret; // Zlib return code
302     int i;
303     int lvl = 9;
304     int prev_size, prev_offset;
305
306     /* Entropy-based score tables for comparing blocks.
307      * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
308      * Scores are nonnegative, lower is better.
309      */
310     for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK; i++)
311         c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK)) * 256;
312
313     c->avctx = avctx;
314
315     c->curfrm = 0;
316     c->keyint = avctx->keyint_min;
317
318     /* Motion estimation range: maximum distance is -64..63 */
319     c->lrange = c->urange = 8;
320     if(avctx->me_range > 0){
321         c->lrange = FFMIN(avctx->me_range, 64);
322         c->urange = FFMIN(avctx->me_range, 63);
323     }
324
325     if(avctx->compression_level >= 0)
326         lvl = avctx->compression_level;
327     if(lvl < 0 || lvl > 9){
328         av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
329         return AVERROR(EINVAL);
330     }
331
332     // Needed if zlib unused or init aborted before deflateInit
333     memset(&c->zstream, 0, sizeof(z_stream));
334     c->comp_size = avctx->width * avctx->height + 1024 +
335         ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
336     if (!(c->work_buf = av_malloc(c->comp_size))) {
337         av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
338         return AVERROR(ENOMEM);
339     }
340     /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
341     c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
342                            ((c->comp_size + 63) >> 6) + 11;
343
344     /* Allocate compression buffer */
345     if (!(c->comp_buf = av_malloc(c->comp_size))) {
346         av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
347         return AVERROR(ENOMEM);
348     }
349
350     /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
351      * - The image should be padded with `lrange` rows before and `urange` rows
352      *   after.
353      * - The stride should be padded with `lrange` pixels, then rounded up to a
354      *   multiple of 16 bytes.
355      * - The first row should also be padded with `lrange` pixels before, then
356      *   aligned up to a multiple of 16 bytes.
357      */
358     c->pstride = FFALIGN(avctx->width + c->lrange, 16);
359     prev_size = FFALIGN(c->lrange, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
360     prev_offset = FFALIGN(c->lrange, 16) + c->pstride * c->lrange;
361     if (!(c->prev_buf = av_mallocz(prev_size))) {
362         av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
363         return AVERROR(ENOMEM);
364     }
365     c->prev = c->prev_buf + prev_offset;
366
367     c->zstream.zalloc = Z_NULL;
368     c->zstream.zfree = Z_NULL;
369     c->zstream.opaque = Z_NULL;
370     zret = deflateInit(&c->zstream, lvl);
371     if (zret != Z_OK) {
372         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
373         return -1;
374     }
375
376     return 0;
377 }
378
379 AVCodec ff_zmbv_encoder = {
380     .name           = "zmbv",
381     .long_name      = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
382     .type           = AVMEDIA_TYPE_VIDEO,
383     .id             = AV_CODEC_ID_ZMBV,
384     .priv_data_size = sizeof(ZmbvEncContext),
385     .init           = encode_init,
386     .encode2        = encode_frame,
387     .close          = encode_end,
388     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE },
389 };