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