4285
|
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 /**
|
|
24 * @file zmbvenc.c
|
|
25 * Zip Motion Blocks Video encoder
|
|
26 */
|
|
27
|
|
28 #include <stdio.h>
|
|
29 #include <stdlib.h>
|
|
30
|
|
31 #include "avcodec.h"
|
|
32
|
|
33 #include <zlib.h>
|
|
34
|
|
35 #define ZMBV_KEYFRAME 1
|
|
36 #define ZMBV_DELTAPAL 2
|
|
37
|
|
38 #define ZMBV_BLOCK 16
|
|
39
|
|
40 /**
|
|
41 * Encoder context
|
|
42 */
|
|
43 typedef struct ZmbvEncContext {
|
|
44 AVCodecContext *avctx;
|
|
45 AVFrame pic;
|
|
46
|
|
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 /** Block comparing function
|
|
59 * XXX should be optimized and moved to DSPContext
|
|
60 * TODO handle out of edge ME
|
|
61 */
|
|
62 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
|
|
63 {
|
|
64 int sum = 0;
|
|
65 int i, j;
|
|
66
|
|
67 for(j = 0; j < bh; j++){
|
|
68 for(i = 0; i < bw; i++)
|
|
69 sum += src[i] ^ src2[i];
|
|
70 src += stride;
|
|
71 src2 += stride2;
|
|
72 }
|
|
73 return sum;
|
|
74 }
|
|
75
|
|
76 /** Motion estimation function
|
|
77 * TODO make better ME decisions
|
|
78 */
|
|
79 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
|
|
80 int x, int y, int *mx, int *my)
|
|
81 {
|
4647
|
82 int dx, dy, tx, ty, tv, bv, bw, bh;
|
4285
|
83
|
|
84 *mx = *my = 0;
|
4647
|
85 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
|
|
86 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
|
|
87 bv = block_cmp(src, sstride, prev, pstride, bw, bh);
|
4285
|
88 if(!bv) return 0;
|
4647
|
89 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
|
|
90 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
|
4285
|
91 if(tx == x && ty == y) continue; // we already tested this block
|
|
92 dx = tx - x;
|
|
93 dy = ty - y;
|
4647
|
94 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh);
|
4285
|
95 if(tv < bv){
|
|
96 bv = tv;
|
|
97 *mx = dx;
|
|
98 *my = dy;
|
|
99 if(!bv) return 0;
|
|
100 }
|
|
101 }
|
|
102 }
|
|
103 return bv;
|
|
104 }
|
|
105
|
|
106 static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
|
|
107 {
|
4827
|
108 ZmbvEncContext * const c = avctx->priv_data;
|
4285
|
109 AVFrame *pict = data;
|
|
110 AVFrame * const p = &c->pic;
|
|
111 uint8_t *src, *prev;
|
|
112 uint32_t *palptr;
|
|
113 int zret = Z_OK;
|
|
114 int len = 0;
|
|
115 int keyframe, chpal;
|
|
116 int fl;
|
|
117 int work_size = 0;
|
|
118 int bw, bh;
|
|
119 int i, j;
|
|
120
|
|
121 keyframe = !c->curfrm;
|
4287
|
122 c->curfrm++;
|
4285
|
123 if(c->curfrm == c->keyint)
|
|
124 c->curfrm = 0;
|
|
125 *p = *pict;
|
|
126 p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
|
|
127 p->key_frame= keyframe;
|
|
128 chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
|
|
129
|
|
130 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
|
|
131 *buf++ = fl; len++;
|
|
132 if(keyframe){
|
|
133 deflateReset(&c->zstream);
|
|
134 *buf++ = 0; len++; // hi ver
|
|
135 *buf++ = 1; len++; // lo ver
|
|
136 *buf++ = 1; len++; // comp
|
|
137 *buf++ = 4; len++; // format - 8bpp
|
|
138 *buf++ = ZMBV_BLOCK; len++; // block width
|
|
139 *buf++ = ZMBV_BLOCK; len++; // block height
|
|
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++){
|
5089
|
147 AV_WB24(tpal, palptr[i]);
|
4285
|
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++){
|
5089
|
159 AV_WB24(c->pal+(i*3), palptr[i]);
|
4285
|
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;
|
|
171 uint8_t *tsrc, *tprev;
|
|
172 uint8_t *mv;
|
|
173 int mx, my, bv;
|
|
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 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
|
|
190 mv[0] = (mx << 1) | !!bv;
|
|
191 mv[1] = my << 1;
|
|
192 tprev += mx + my * c->pstride;
|
|
193 if(bv){
|
|
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 c->zstream.next_in = c->work_buf;
|
|
216 c->zstream.avail_in = work_size;
|
|
217 c->zstream.total_in = 0;
|
|
218
|
|
219 c->zstream.next_out = c->comp_buf;
|
|
220 c->zstream.avail_out = c->comp_size;
|
|
221 c->zstream.total_out = 0;
|
|
222 if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
|
|
223 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
|
|
224 return -1;
|
|
225 }
|
|
226
|
|
227 memcpy(buf, c->comp_buf, c->zstream.total_out);
|
|
228 return len + c->zstream.total_out;
|
|
229 }
|
|
230
|
|
231
|
|
232 /**
|
|
233 * Init zmbv encoder
|
|
234 */
|
|
235 static int encode_init(AVCodecContext *avctx)
|
|
236 {
|
4827
|
237 ZmbvEncContext * const c = avctx->priv_data;
|
4285
|
238 int zret; // Zlib return code
|
|
239 int lvl = 9;
|
|
240
|
|
241 c->avctx = avctx;
|
|
242
|
|
243 c->pic.data[0] = NULL;
|
|
244 c->curfrm = 0;
|
|
245 c->keyint = avctx->keyint_min;
|
|
246 c->range = 8;
|
|
247 if(avctx->me_range > 0)
|
|
248 c->range = FFMIN(avctx->me_range, 127);
|
|
249
|
|
250 if(avctx->compression_level >= 0)
|
|
251 lvl = avctx->compression_level;
|
|
252 if(lvl < 0 || lvl > 9){
|
|
253 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
|
|
254 return -1;
|
|
255 }
|
|
256
|
|
257 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
|
|
258 return -1;
|
|
259 }
|
|
260
|
|
261 // Needed if zlib unused or init aborted before deflateInit
|
|
262 memset(&(c->zstream), 0, sizeof(z_stream));
|
|
263 c->comp_size = avctx->width * avctx->height + 1024 +
|
|
264 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
|
|
265 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
|
|
266 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
|
|
267 return -1;
|
|
268 }
|
|
269 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
|
|
270 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
|
|
271 ((c->comp_size + 63) >> 6) + 11;
|
|
272
|
|
273 /* Allocate compression buffer */
|
|
274 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
|
|
275 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
|
|
276 return -1;
|
|
277 }
|
|
278 c->pstride = (avctx->width + 15) & ~15;
|
|
279 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
|
|
280 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
|
|
281 return -1;
|
|
282 }
|
|
283
|
|
284 c->zstream.zalloc = Z_NULL;
|
|
285 c->zstream.zfree = Z_NULL;
|
|
286 c->zstream.opaque = Z_NULL;
|
|
287 zret = deflateInit(&(c->zstream), lvl);
|
|
288 if (zret != Z_OK) {
|
|
289 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
|
290 return -1;
|
|
291 }
|
|
292
|
|
293 return 0;
|
|
294 }
|
|
295
|
|
296
|
|
297
|
|
298 /**
|
4368
|
299 * Uninit zmbv encoder
|
4285
|
300 */
|
|
301 static int encode_end(AVCodecContext *avctx)
|
|
302 {
|
4827
|
303 ZmbvEncContext * const c = avctx->priv_data;
|
4285
|
304
|
|
305 av_freep(&c->comp_buf);
|
|
306 av_freep(&c->work_buf);
|
|
307
|
|
308 deflateEnd(&(c->zstream));
|
|
309 av_freep(&c->prev);
|
|
310
|
|
311 return 0;
|
|
312 }
|
|
313
|
|
314 AVCodec zmbv_encoder = {
|
|
315 "zmbv",
|
|
316 CODEC_TYPE_VIDEO,
|
|
317 CODEC_ID_ZMBV,
|
|
318 sizeof(ZmbvEncContext),
|
|
319 encode_init,
|
|
320 encode_frame,
|
|
321 encode_end,
|
|
322 .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},
|
|
323 };
|