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