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