FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/zmbvenc.c
Date: 2024-03-29 11:55:30
Exec Total Coverage
Lines: 0 194 0.0%
Functions: 0 5 0.0%
Branches: 0 99 0.0%

Line Branch Exec Source
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
24 * Zip Motion Blocks Video encoder
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "libavutil/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "avcodec.h"
33 #include "codec_internal.h"
34 #include "encode.h"
35 #include "zlib_wrapper.h"
36
37 #include <zlib.h>
38
39 /* Frame header flags */
40 #define ZMBV_KEYFRAME 1
41 #define ZMBV_DELTAPAL 2
42
43 /* Motion block width/height (maximum allowed value is 255)
44 * Note: histogram datatype in block_cmp() must be big enough to hold values
45 * up to (4 * ZMBV_BLOCK * ZMBV_BLOCK)
46 */
47 #define ZMBV_BLOCK 16
48
49 /* Keyframe header format values */
50 enum ZmbvFormat {
51 ZMBV_FMT_NONE = 0,
52 ZMBV_FMT_1BPP = 1,
53 ZMBV_FMT_2BPP = 2,
54 ZMBV_FMT_4BPP = 3,
55 ZMBV_FMT_8BPP = 4,
56 ZMBV_FMT_15BPP = 5,
57 ZMBV_FMT_16BPP = 6,
58 ZMBV_FMT_24BPP = 7,
59 ZMBV_FMT_32BPP = 8
60 };
61
62 /**
63 * Encoder context
64 */
65 typedef struct ZmbvEncContext {
66 AVCodecContext *avctx;
67
68 int lrange, urange;
69 uint8_t *comp_buf, *work_buf;
70 uint8_t pal[768];
71 uint32_t pal2[256]; //for quick comparisons
72 uint8_t *prev, *prev_buf;
73 int pstride;
74 int comp_size;
75 int keyint, curfrm;
76 int bypp;
77 enum ZmbvFormat fmt;
78 FFZStream zstream;
79
80 int score_tab[ZMBV_BLOCK * ZMBV_BLOCK * 4 + 1];
81 } ZmbvEncContext;
82
83
84 /** Block comparing function
85 * XXX should be optimized and moved to DSPContext
86 */
87 static inline int block_cmp(ZmbvEncContext *c, const uint8_t *src, int stride,
88 const uint8_t *src2, int stride2, int bw, int bh,
89 int *xored)
90 {
91 int sum = 0;
92 int i, j;
93 uint16_t histogram[256] = {0};
94 int bw_bytes = bw * c->bypp;
95
96 /* Build frequency histogram of byte values for src[] ^ src2[] */
97 for(j = 0; j < bh; j++){
98 for(i = 0; i < bw_bytes; i++){
99 int t = src[i] ^ src2[i];
100 histogram[t]++;
101 }
102 src += stride;
103 src2 += stride2;
104 }
105
106 /* If not all the xored values were 0, then the blocks are different */
107 *xored = (histogram[0] < bw_bytes * bh);
108
109 /* Exit early if blocks are equal */
110 if (!*xored) return 0;
111
112 /* Sum the entropy of all values */
113 for(i = 0; i < 256; i++)
114 sum += c->score_tab[histogram[i]];
115
116 return sum;
117 }
118
119 /** Motion estimation function
120 * TODO make better ME decisions
121 */
122 static int zmbv_me(ZmbvEncContext *c, const uint8_t *src, int sstride, const uint8_t *prev,
123 int pstride, int x, int y, int *mx, int *my, int *xored)
124 {
125 int dx, dy, txored, tv, bv, bw, bh;
126 int mx0, my0;
127
128 mx0 = *mx;
129 my0 = *my;
130 bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
131 bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
132
133 /* Try (0,0) */
134 bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
135 *mx = *my = 0;
136 if(!bv) return 0;
137
138 /* Try previous block's MV (if not 0,0) */
139 if (mx0 || my0){
140 tv = block_cmp(c, src, sstride, prev + mx0 * c->bypp + my0 * pstride, pstride, bw, bh, &txored);
141 if(tv < bv){
142 bv = tv;
143 *mx = mx0;
144 *my = my0;
145 *xored = txored;
146 if(!bv) return 0;
147 }
148 }
149
150 /* Try other MVs from top-to-bottom, left-to-right */
151 for(dy = -c->lrange; dy <= c->urange; dy++){
152 for(dx = -c->lrange; dx <= c->urange; dx++){
153 if(!dx && !dy) continue; // we already tested this block
154 if(dx == mx0 && dy == my0) continue; // this one too
155 tv = block_cmp(c, src, sstride, prev + dx * c->bypp + dy * pstride, pstride, bw, bh, &txored);
156 if(tv < bv){
157 bv = tv;
158 *mx = dx;
159 *my = dy;
160 *xored = txored;
161 if(!bv) return 0;
162 }
163 }
164 }
165 return bv;
166 }
167
168 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
169 const AVFrame *pict, int *got_packet)
170 {
171 ZmbvEncContext * const c = avctx->priv_data;
172 z_stream *const zstream = &c->zstream.zstream;
173 const AVFrame * const p = pict;
174 const uint8_t *src;
175 uint8_t *prev, *buf;
176 uint32_t *palptr;
177 int keyframe, chpal;
178 int fl;
179 int work_size = 0, pkt_size;
180 int bw, bh;
181 int i, j, ret;
182
183 keyframe = !c->curfrm;
184 c->curfrm++;
185 if(c->curfrm == c->keyint)
186 c->curfrm = 0;
187
188 palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
189 chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
190
191 src = p->data[0];
192 prev = c->prev;
193 if(chpal){
194 uint8_t tpal[3];
195 for(i = 0; i < 256; i++){
196 AV_WB24(tpal, palptr[i]);
197 c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
198 c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
199 c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
200 c->pal[i * 3 + 0] = tpal[0];
201 c->pal[i * 3 + 1] = tpal[1];
202 c->pal[i * 3 + 2] = tpal[2];
203 }
204 memcpy(c->pal2, palptr, 1024);
205 }
206 if(keyframe){
207 if (palptr){
208 for(i = 0; i < 256; i++){
209 AV_WB24(c->pal+(i*3), palptr[i]);
210 }
211 memcpy(c->work_buf, c->pal, 768);
212 memcpy(c->pal2, palptr, 1024);
213 work_size = 768;
214 }
215 for(i = 0; i < avctx->height; i++){
216 memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
217 src += p->linesize[0];
218 work_size += avctx->width * c->bypp;
219 }
220 }else{
221 int x, y, bh2, bw2, xored;
222 const uint8_t *tsrc, *tprev;
223 uint8_t *mv;
224 int mx = 0, my = 0;
225
226 bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
227 bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
228 mv = c->work_buf + work_size;
229 memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
230 work_size += (bw * bh * 2 + 3) & ~3;
231 /* for now just XOR'ing */
232 for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
233 bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
234 for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
235 bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
236
237 tsrc = src + x * c->bypp;
238 tprev = prev + x * c->bypp;
239
240 zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
241 mv[0] = (mx * 2) | !!xored;
242 mv[1] = my * 2;
243 tprev += mx * c->bypp + my * c->pstride;
244 if(xored){
245 for(j = 0; j < bh2; j++){
246 for(i = 0; i < bw2 * c->bypp; i++)
247 c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
248 tsrc += p->linesize[0];
249 tprev += c->pstride;
250 }
251 }
252 }
253 src += p->linesize[0] * ZMBV_BLOCK;
254 prev += c->pstride * ZMBV_BLOCK;
255 }
256 }
257 /* save the previous frame */
258 src = p->data[0];
259 prev = c->prev;
260 for(i = 0; i < avctx->height; i++){
261 memcpy(prev, src, avctx->width * c->bypp);
262 prev += c->pstride;
263 src += p->linesize[0];
264 }
265
266 if (keyframe)
267 deflateReset(zstream);
268
269 zstream->next_in = c->work_buf;
270 zstream->avail_in = work_size;
271 zstream->total_in = 0;
272
273 zstream->next_out = c->comp_buf;
274 zstream->avail_out = c->comp_size;
275 zstream->total_out = 0;
276 if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
277 av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
278 return -1;
279 }
280
281 pkt_size = zstream->total_out + 1 + 6 * keyframe;
282 if ((ret = ff_get_encode_buffer(avctx, pkt, pkt_size, 0)) < 0)
283 return ret;
284 buf = pkt->data;
285
286 fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
287 *buf++ = fl;
288 if (keyframe) {
289 *buf++ = 0; // hi ver
290 *buf++ = 1; // lo ver
291 *buf++ = 1; // comp
292 *buf++ = c->fmt; // format
293 *buf++ = ZMBV_BLOCK; // block width
294 *buf++ = ZMBV_BLOCK; // block height
295 pkt->flags |= AV_PKT_FLAG_KEY;
296 }
297 memcpy(buf, c->comp_buf, zstream->total_out);
298
299 *got_packet = 1;
300
301 return 0;
302 }
303
304 static av_cold int encode_end(AVCodecContext *avctx)
305 {
306 ZmbvEncContext * const c = avctx->priv_data;
307
308 av_freep(&c->comp_buf);
309 av_freep(&c->work_buf);
310
311 av_freep(&c->prev_buf);
312 ff_deflate_end(&c->zstream);
313
314 return 0;
315 }
316
317 /**
318 * Init zmbv encoder
319 */
320 static av_cold int encode_init(AVCodecContext *avctx)
321 {
322 ZmbvEncContext * const c = avctx->priv_data;
323 int i;
324 int lvl = 9;
325 int prev_size, prev_offset;
326
327 switch (avctx->pix_fmt) {
328 case AV_PIX_FMT_PAL8:
329 c->fmt = ZMBV_FMT_8BPP;
330 c->bypp = 1;
331 break;
332 case AV_PIX_FMT_RGB555LE:
333 c->fmt = ZMBV_FMT_15BPP;
334 c->bypp = 2;
335 break;
336 case AV_PIX_FMT_RGB565LE:
337 c->fmt = ZMBV_FMT_16BPP;
338 c->bypp = 2;
339 break;
340 #ifdef ZMBV_ENABLE_24BPP
341 case AV_PIX_FMT_BGR24:
342 c->fmt = ZMBV_FMT_24BPP;
343 c->bypp = 3;
344 break;
345 #endif //ZMBV_ENABLE_24BPP
346 case AV_PIX_FMT_BGR0:
347 c->fmt = ZMBV_FMT_32BPP;
348 c->bypp = 4;
349 break;
350 }
351
352 /* Entropy-based score tables for comparing blocks.
353 * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
354 * Scores are nonnegative, lower is better.
355 */
356 for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
357 c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
358
359 c->avctx = avctx;
360
361 c->curfrm = 0;
362 c->keyint = avctx->keyint_min;
363
364 /* Motion estimation range: maximum distance is -64..63 */
365 c->lrange = c->urange = 8;
366 if(avctx->me_range > 0){
367 c->lrange = FFMIN(avctx->me_range, 64);
368 c->urange = FFMIN(avctx->me_range, 63);
369 }
370
371 if(avctx->compression_level >= 0)
372 lvl = avctx->compression_level;
373 if(lvl < 0 || lvl > 9){
374 av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
375 return AVERROR(EINVAL);
376 }
377
378 c->comp_size = avctx->width * c->bypp * avctx->height + 1024 +
379 ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
380 if (!(c->work_buf = av_malloc(c->comp_size))) {
381 av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
382 return AVERROR(ENOMEM);
383 }
384 /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
385 c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
386 ((c->comp_size + 63) >> 6) + 11;
387
388 /* Allocate compression buffer */
389 if (!(c->comp_buf = av_malloc(c->comp_size))) {
390 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
391 return AVERROR(ENOMEM);
392 }
393
394 /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
395 * - The image should be padded with `lrange` rows before and `urange` rows
396 * after.
397 * - The stride should be padded with `lrange` pixels, then rounded up to a
398 * multiple of 16 bytes.
399 * - The first row should also be padded with `lrange` pixels before, then
400 * aligned up to a multiple of 16 bytes.
401 */
402 c->pstride = FFALIGN((avctx->width + c->lrange) * c->bypp, 16);
403 prev_size = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
404 prev_offset = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * c->lrange;
405 if (!(c->prev_buf = av_mallocz(prev_size))) {
406 av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
407 return AVERROR(ENOMEM);
408 }
409 c->prev = c->prev_buf + prev_offset;
410
411 return ff_deflate_init(&c->zstream, lvl, avctx);
412 }
413
414 const FFCodec ff_zmbv_encoder = {
415 .p.name = "zmbv",
416 CODEC_LONG_NAME("Zip Motion Blocks Video"),
417 .p.type = AVMEDIA_TYPE_VIDEO,
418 .p.id = AV_CODEC_ID_ZMBV,
419 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
420 .priv_data_size = sizeof(ZmbvEncContext),
421 .init = encode_init,
422 FF_CODEC_ENCODE_CB(encode_frame),
423 .close = encode_end,
424 .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
425 AV_PIX_FMT_RGB555LE,
426 AV_PIX_FMT_RGB565LE,
427 #ifdef ZMBV_ENABLE_24BPP
428 AV_PIX_FMT_BGR24,
429 #endif //ZMBV_ENABLE_24BPP
430 AV_PIX_FMT_BGR0,
431 AV_PIX_FMT_NONE },
432 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
433 };
434