Remove offset_t typedef and use int64_t directly instead.
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavcodec / zmbvenc.c
blob79478de510bd6fc9253fdadc88b1d71fa67cbfbe
1 /*
2 * Zip Motion Blocks Video (ZMBV) encoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file zmbvenc.c
24 * Zip Motion Blocks Video encoder
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "avcodec.h"
32 #include <zlib.h>
34 #define ZMBV_KEYFRAME 1
35 #define ZMBV_DELTAPAL 2
37 #define ZMBV_BLOCK 16
39 /**
40 * Encoder context
42 typedef struct ZmbvEncContext {
43 AVCodecContext *avctx;
44 AVFrame pic;
46 int range;
47 uint8_t *comp_buf, *work_buf;
48 uint8_t pal[768];
49 uint32_t pal2[256]; //for quick comparisons
50 uint8_t *prev;
51 int pstride;
52 int comp_size;
53 int keyint, curfrm;
54 z_stream zstream;
55 } ZmbvEncContext;
57 static int score_tab[256];
59 /** Block comparing function
60 * XXX should be optimized and moved to DSPContext
61 * TODO handle out of edge ME
63 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
65 int sum = 0;
66 int i, j;
67 uint8_t histogram[256]={0};
69 for(j = 0; j < bh; j++){
70 for(i = 0; i < bw; i++)
71 histogram[src[i] ^ src2[i]]++;
72 src += stride;
73 src2 += stride2;
76 for(i=1; i<256; i++)
77 sum+= score_tab[histogram[i]];
79 return sum;
82 /** Motion estimation function
83 * TODO make better ME decisions
85 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
86 int x, int y, int *mx, int *my)
88 int dx, dy, tx, ty, tv, bv, bw, bh;
90 *mx = *my = 0;
91 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
92 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
93 bv = block_cmp(src, sstride, prev, pstride, bw, bh);
94 if(!bv) return 0;
95 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
96 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
97 if(tx == x && ty == y) continue; // we already tested this block
98 dx = tx - x;
99 dy = ty - y;
100 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh);
101 if(tv < bv){
102 bv = tv;
103 *mx = dx;
104 *my = dy;
105 if(!bv) return 0;
109 return bv;
112 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
114 ZmbvEncContext * const c = avctx->priv_data;
115 AVFrame *pict = data;
116 AVFrame * const p = &c->pic;
117 uint8_t *src, *prev;
118 uint32_t *palptr;
119 int zret = Z_OK;
120 int len = 0;
121 int keyframe, chpal;
122 int fl;
123 int work_size = 0;
124 int bw, bh;
125 int i, j;
127 keyframe = !c->curfrm;
128 c->curfrm++;
129 if(c->curfrm == c->keyint)
130 c->curfrm = 0;
131 *p = *pict;
132 p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
133 p->key_frame= keyframe;
134 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
136 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
137 *buf++ = fl; len++;
138 if(keyframe){
139 deflateReset(&c->zstream);
140 *buf++ = 0; len++; // hi ver
141 *buf++ = 1; len++; // lo ver
142 *buf++ = 1; len++; // comp
143 *buf++ = 4; len++; // format - 8bpp
144 *buf++ = ZMBV_BLOCK; len++; // block width
145 *buf++ = ZMBV_BLOCK; len++; // block height
147 palptr = (uint32_t*)p->data[1];
148 src = p->data[0];
149 prev = c->prev;
150 if(chpal){
151 uint8_t tpal[3];
152 for(i = 0; i < 256; i++){
153 AV_WB24(tpal, palptr[i]);
154 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
155 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
156 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
157 c->pal[i * 3 + 0] = tpal[0];
158 c->pal[i * 3 + 1] = tpal[1];
159 c->pal[i * 3 + 2] = tpal[2];
161 memcpy(c->pal2, p->data[1], 1024);
163 if(keyframe){
164 for(i = 0; i < 256; i++){
165 AV_WB24(c->pal+(i*3), palptr[i]);
167 memcpy(c->work_buf, c->pal, 768);
168 memcpy(c->pal2, p->data[1], 1024);
169 work_size = 768;
170 for(i = 0; i < avctx->height; i++){
171 memcpy(c->work_buf + work_size, src, avctx->width);
172 src += p->linesize[0];
173 work_size += avctx->width;
175 }else{
176 int x, y, bh2, bw2;
177 uint8_t *tsrc, *tprev;
178 uint8_t *mv;
179 int mx, my, bv;
181 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
182 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
183 mv = c->work_buf + work_size;
184 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
185 work_size += (bw * bh * 2 + 3) & ~3;
186 /* for now just XOR'ing */
187 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
188 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
189 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
190 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
192 tsrc = src + x;
193 tprev = prev + x;
195 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
196 mv[0] = (mx << 1) | !!bv;
197 mv[1] = my << 1;
198 tprev += mx + my * c->pstride;
199 if(bv){
200 for(j = 0; j < bh2; j++){
201 for(i = 0; i < bw2; i++)
202 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
203 tsrc += p->linesize[0];
204 tprev += c->pstride;
208 src += p->linesize[0] * ZMBV_BLOCK;
209 prev += c->pstride * ZMBV_BLOCK;
212 /* save the previous frame */
213 src = p->data[0];
214 prev = c->prev;
215 for(i = 0; i < avctx->height; i++){
216 memcpy(prev, src, avctx->width);
217 prev += c->pstride;
218 src += p->linesize[0];
221 c->zstream.next_in = c->work_buf;
222 c->zstream.avail_in = work_size;
223 c->zstream.total_in = 0;
225 c->zstream.next_out = c->comp_buf;
226 c->zstream.avail_out = c->comp_size;
227 c->zstream.total_out = 0;
228 if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
229 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
230 return -1;
233 memcpy(buf, c->comp_buf, c->zstream.total_out);
234 return len + c->zstream.total_out;
239 * Init zmbv encoder
241 static av_cold int encode_init(AVCodecContext *avctx)
243 ZmbvEncContext * const c = avctx->priv_data;
244 int zret; // Zlib return code
245 int i;
246 int lvl = 9;
248 for(i=1; i<256; i++)
249 score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
251 c->avctx = avctx;
253 c->pic.data[0] = NULL;
254 c->curfrm = 0;
255 c->keyint = avctx->keyint_min;
256 c->range = 8;
257 if(avctx->me_range > 0)
258 c->range = FFMIN(avctx->me_range, 127);
260 if(avctx->compression_level >= 0)
261 lvl = avctx->compression_level;
262 if(lvl < 0 || lvl > 9){
263 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
264 return -1;
267 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
268 return -1;
271 // Needed if zlib unused or init aborted before deflateInit
272 memset(&(c->zstream), 0, sizeof(z_stream));
273 c->comp_size = avctx->width * avctx->height + 1024 +
274 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
275 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
276 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
277 return -1;
279 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
280 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
281 ((c->comp_size + 63) >> 6) + 11;
283 /* Allocate compression buffer */
284 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
285 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
286 return -1;
288 c->pstride = (avctx->width + 15) & ~15;
289 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
290 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
291 return -1;
294 c->zstream.zalloc = Z_NULL;
295 c->zstream.zfree = Z_NULL;
296 c->zstream.opaque = Z_NULL;
297 zret = deflateInit(&(c->zstream), lvl);
298 if (zret != Z_OK) {
299 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
300 return -1;
303 avctx->coded_frame = (AVFrame*)&c->pic;
305 return 0;
311 * Uninit zmbv encoder
313 static av_cold int encode_end(AVCodecContext *avctx)
315 ZmbvEncContext * const c = avctx->priv_data;
317 av_freep(&c->comp_buf);
318 av_freep(&c->work_buf);
320 deflateEnd(&(c->zstream));
321 av_freep(&c->prev);
323 return 0;
326 AVCodec zmbv_encoder = {
327 "zmbv",
328 CODEC_TYPE_VIDEO,
329 CODEC_ID_ZMBV,
330 sizeof(ZmbvEncContext),
331 encode_init,
332 encode_frame,
333 encode_end,
334 .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
335 .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),