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 "common.h"
|
|
32 #include "avcodec.h"
|
|
33
|
|
34 #ifdef CONFIG_ZLIB
|
|
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 AVFrame pic;
|
|
48
|
|
49 int range;
|
|
50 uint8_t *comp_buf, *work_buf;
|
|
51 uint8_t pal[768];
|
|
52 uint32_t pal2[256]; //for quick comparisons
|
|
53 uint8_t *prev;
|
|
54 int pstride;
|
|
55 int comp_size;
|
|
56 int keyint, curfrm;
|
|
57 z_stream zstream;
|
|
58 } ZmbvEncContext;
|
|
59
|
|
60 /** Block comparing function
|
|
61 * XXX should be optimized and moved to DSPContext
|
|
62 * TODO handle out of edge ME
|
|
63 */
|
|
64 static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
|
|
65 {
|
|
66 int sum = 0;
|
|
67 int i, j;
|
|
68
|
|
69 for(j = 0; j < bh; j++){
|
|
70 for(i = 0; i < bw; i++)
|
|
71 sum += src[i] ^ src2[i];
|
|
72 src += stride;
|
|
73 src2 += stride2;
|
|
74 }
|
|
75 return sum;
|
|
76 }
|
|
77
|
|
78 /** Motion estimation function
|
|
79 * TODO make better ME decisions
|
|
80 */
|
|
81 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
|
|
82 int x, int y, int *mx, int *my)
|
|
83 {
|
|
84 int dx, dy, tx, ty, tv, bv;
|
|
85
|
|
86 *mx = *my = 0;
|
|
87 bv = block_cmp(src, sstride, prev, pstride, ZMBV_BLOCK, ZMBV_BLOCK);
|
|
88 if(!bv) return 0;
|
|
89 for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - ZMBV_BLOCK); ty++){
|
|
90 for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - ZMBV_BLOCK); tx++){
|
|
91 if(tx == x && ty == y) continue; // we already tested this block
|
|
92 dx = tx - x;
|
|
93 dy = ty - y;
|
|
94 tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, ZMBV_BLOCK, ZMBV_BLOCK);
|
|
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 {
|
|
108 ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
|
|
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++){
|
|
147 tpal[0] = palptr[i] >> 16;
|
|
148 tpal[1] = palptr[i] >> 8;
|
|
149 tpal[2] = palptr[i];
|
|
150 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
|
|
151 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
|
|
152 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
|
|
153 c->pal[i * 3 + 0] = tpal[0];
|
|
154 c->pal[i * 3 + 1] = tpal[1];
|
|
155 c->pal[i * 3 + 2] = tpal[2];
|
|
156 }
|
|
157 memcpy(c->pal2, p->data[1], 1024);
|
|
158 }
|
|
159 if(keyframe){
|
|
160 for(i = 0; i < 256; i++){
|
|
161 c->pal[i*3 + 0] = palptr[i] >> 16;
|
|
162 c->pal[i*3 + 1] = palptr[i] >> 8;
|
|
163 c->pal[i*3 + 2] = palptr[i];
|
|
164 }
|
|
165 memcpy(c->work_buf, c->pal, 768);
|
|
166 memcpy(c->pal2, p->data[1], 1024);
|
|
167 work_size = 768;
|
|
168 for(i = 0; i < avctx->height; i++){
|
|
169 memcpy(c->work_buf + work_size, src, avctx->width);
|
|
170 src += p->linesize[0];
|
|
171 work_size += avctx->width;
|
|
172 }
|
|
173 }else{
|
|
174 int x, y, bh2, bw2;
|
|
175 uint8_t *tsrc, *tprev;
|
|
176 uint8_t *mv;
|
|
177 int mx, my, bv;
|
|
178
|
|
179 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
|
|
180 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
|
|
181 mv = c->work_buf + work_size;
|
|
182 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
|
|
183 work_size += (bw * bh * 2 + 3) & ~3;
|
|
184 /* for now just XOR'ing */
|
|
185 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
|
|
186 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
|
|
187 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
|
|
188 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
|
|
189
|
|
190 tsrc = src + x;
|
|
191 tprev = prev + x;
|
|
192
|
|
193 bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
|
|
194 mv[0] = (mx << 1) | !!bv;
|
|
195 mv[1] = my << 1;
|
|
196 tprev += mx + my * c->pstride;
|
|
197 if(bv){
|
|
198 for(j = 0; j < bh2; j++){
|
|
199 for(i = 0; i < bw2; i++)
|
|
200 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
|
|
201 tsrc += p->linesize[0];
|
|
202 tprev += c->pstride;
|
|
203 }
|
|
204 }
|
|
205 }
|
|
206 src += p->linesize[0] * ZMBV_BLOCK;
|
|
207 prev += c->pstride * ZMBV_BLOCK;
|
|
208 }
|
|
209 }
|
|
210 /* save the previous frame */
|
|
211 src = p->data[0];
|
|
212 prev = c->prev;
|
|
213 for(i = 0; i < avctx->height; i++){
|
|
214 memcpy(prev, src, avctx->width);
|
|
215 prev += c->pstride;
|
|
216 src += p->linesize[0];
|
|
217 }
|
|
218
|
|
219 c->zstream.next_in = c->work_buf;
|
|
220 c->zstream.avail_in = work_size;
|
|
221 c->zstream.total_in = 0;
|
|
222
|
|
223 c->zstream.next_out = c->comp_buf;
|
|
224 c->zstream.avail_out = c->comp_size;
|
|
225 c->zstream.total_out = 0;
|
|
226 if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
|
|
227 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
|
|
228 return -1;
|
|
229 }
|
|
230
|
|
231 memcpy(buf, c->comp_buf, c->zstream.total_out);
|
|
232 return len + c->zstream.total_out;
|
|
233 }
|
|
234
|
|
235
|
|
236 /**
|
|
237 * Init zmbv encoder
|
|
238 */
|
|
239 static int encode_init(AVCodecContext *avctx)
|
|
240 {
|
|
241 ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
|
|
242 int zret; // Zlib return code
|
|
243 int lvl = 9;
|
|
244
|
|
245 c->avctx = avctx;
|
|
246
|
|
247 c->pic.data[0] = NULL;
|
|
248 c->curfrm = 0;
|
|
249 c->keyint = avctx->keyint_min;
|
|
250 c->range = 8;
|
|
251 if(avctx->me_range > 0)
|
|
252 c->range = FFMIN(avctx->me_range, 127);
|
|
253
|
|
254 if(avctx->compression_level >= 0)
|
|
255 lvl = avctx->compression_level;
|
|
256 if(lvl < 0 || lvl > 9){
|
|
257 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
|
|
258 return -1;
|
|
259 }
|
|
260
|
|
261 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
|
|
262 return -1;
|
|
263 }
|
|
264
|
|
265 // Needed if zlib unused or init aborted before deflateInit
|
|
266 memset(&(c->zstream), 0, sizeof(z_stream));
|
|
267 c->comp_size = avctx->width * avctx->height + 1024 +
|
|
268 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
|
|
269 if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
|
|
270 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
|
|
271 return -1;
|
|
272 }
|
|
273 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
|
|
274 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
|
|
275 ((c->comp_size + 63) >> 6) + 11;
|
|
276
|
|
277 /* Allocate compression buffer */
|
|
278 if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
|
|
279 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
|
|
280 return -1;
|
|
281 }
|
|
282 c->pstride = (avctx->width + 15) & ~15;
|
|
283 if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
|
|
284 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
|
|
285 return -1;
|
|
286 }
|
|
287
|
|
288 c->zstream.zalloc = Z_NULL;
|
|
289 c->zstream.zfree = Z_NULL;
|
|
290 c->zstream.opaque = Z_NULL;
|
|
291 zret = deflateInit(&(c->zstream), lvl);
|
|
292 if (zret != Z_OK) {
|
|
293 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
|
294 return -1;
|
|
295 }
|
|
296
|
|
297 return 0;
|
|
298 }
|
|
299
|
|
300
|
|
301
|
|
302 /**
|
4368
|
303 * Uninit zmbv encoder
|
4285
|
304 */
|
|
305 static int encode_end(AVCodecContext *avctx)
|
|
306 {
|
|
307 ZmbvEncContext * const c = (ZmbvEncContext *)avctx->priv_data;
|
|
308
|
|
309 av_freep(&c->comp_buf);
|
|
310 av_freep(&c->work_buf);
|
|
311
|
|
312 deflateEnd(&(c->zstream));
|
|
313 av_freep(&c->prev);
|
|
314
|
|
315 return 0;
|
|
316 }
|
|
317
|
|
318 AVCodec zmbv_encoder = {
|
|
319 "zmbv",
|
|
320 CODEC_TYPE_VIDEO,
|
|
321 CODEC_ID_ZMBV,
|
|
322 sizeof(ZmbvEncContext),
|
|
323 encode_init,
|
|
324 encode_frame,
|
|
325 encode_end,
|
|
326 .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},
|
|
327 };
|
|
328 #endif
|