FFmpeg  4.4.5
zmbvenc.c
Go to the documentation of this file.
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 "internal.h"
34 
35 #include <zlib.h>
36 
37 /* Frame header flags */
38 #define ZMBV_KEYFRAME 1
39 #define ZMBV_DELTAPAL 2
40 
41 /* Motion block width/height (maximum allowed value is 255)
42  * Note: histogram datatype in block_cmp() must be big enough to hold values
43  * up to (4 * ZMBV_BLOCK * ZMBV_BLOCK)
44  */
45 #define ZMBV_BLOCK 16
46 
47 /* Keyframe header format values */
48 enum ZmbvFormat {
57  ZMBV_FMT_32BPP = 8
58 };
59 
60 /**
61  * Encoder context
62  */
63 typedef struct ZmbvEncContext {
65 
66  int lrange, urange;
68  uint8_t pal[768];
69  uint32_t pal2[256]; //for quick comparisons
71  int pstride;
72  int comp_size;
73  int keyint, curfrm;
74  int bypp;
75  enum ZmbvFormat fmt;
77  z_stream zstream;
78 
81 
82 
83 /** Block comparing function
84  * XXX should be optimized and moved to DSPContext
85  */
86 static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
87  uint8_t *src2, int stride2, int bw, int bh,
88  int *xored)
89 {
90  int sum = 0;
91  int i, j;
92  uint16_t histogram[256] = {0};
93  int bw_bytes = bw * c->bypp;
94 
95  /* Build frequency histogram of byte values for src[] ^ src2[] */
96  for(j = 0; j < bh; j++){
97  for(i = 0; i < bw_bytes; i++){
98  int t = src[i] ^ src2[i];
99  histogram[t]++;
100  }
101  src += stride;
102  src2 += stride2;
103  }
104 
105  /* If not all the xored values were 0, then the blocks are different */
106  *xored = (histogram[0] < bw_bytes * bh);
107 
108  /* Exit early if blocks are equal */
109  if (!*xored) return 0;
110 
111  /* Sum the entropy of all values */
112  for(i = 0; i < 256; i++)
113  sum += c->score_tab[histogram[i]];
114 
115  return sum;
116 }
117 
118 /** Motion estimation function
119  * TODO make better ME decisions
120  */
121 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
122  int pstride, int x, int y, int *mx, int *my, int *xored)
123 {
124  int dx, dy, txored, tv, bv, bw, bh;
125  int mx0, my0;
126 
127  mx0 = *mx;
128  my0 = *my;
129  bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
130  bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
131 
132  /* Try (0,0) */
133  bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
134  *mx = *my = 0;
135  if(!bv) return 0;
136 
137  /* Try previous block's MV (if not 0,0) */
138  if (mx0 || my0){
139  tv = block_cmp(c, src, sstride, prev + mx0 * c->bypp + my0 * pstride, pstride, bw, bh, &txored);
140  if(tv < bv){
141  bv = tv;
142  *mx = mx0;
143  *my = my0;
144  *xored = txored;
145  if(!bv) return 0;
146  }
147  }
148 
149  /* Try other MVs from top-to-bottom, left-to-right */
150  for(dy = -c->lrange; dy <= c->urange; dy++){
151  for(dx = -c->lrange; dx <= c->urange; dx++){
152  if(!dx && !dy) continue; // we already tested this block
153  if(dx == mx0 && dy == my0) continue; // this one too
154  tv = block_cmp(c, src, sstride, prev + dx * c->bypp + dy * pstride, pstride, bw, bh, &txored);
155  if(tv < bv){
156  bv = tv;
157  *mx = dx;
158  *my = dy;
159  *xored = txored;
160  if(!bv) return 0;
161  }
162  }
163  }
164  return bv;
165 }
166 
168  const AVFrame *pict, int *got_packet)
169 {
170  ZmbvEncContext * const c = avctx->priv_data;
171  const AVFrame * const p = pict;
172  uint8_t *src, *prev, *buf;
173  uint32_t *palptr;
174  int keyframe, chpal;
175  int fl;
176  int work_size = 0, pkt_size;
177  int bw, bh;
178  int i, j, ret;
179 
180  keyframe = !c->curfrm;
181  c->curfrm++;
182  if(c->curfrm == c->keyint)
183  c->curfrm = 0;
184 #if FF_API_CODED_FRAME
187  avctx->coded_frame->key_frame = keyframe;
189 #endif
190 
191  palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
192  chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
193 
194  src = p->data[0];
195  prev = c->prev;
196  if(chpal){
197  uint8_t tpal[3];
198  for(i = 0; i < 256; i++){
199  AV_WB24(tpal, palptr[i]);
200  c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
201  c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
202  c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
203  c->pal[i * 3 + 0] = tpal[0];
204  c->pal[i * 3 + 1] = tpal[1];
205  c->pal[i * 3 + 2] = tpal[2];
206  }
207  memcpy(c->pal2, palptr, 1024);
208  }
209  if(keyframe){
210  if (palptr){
211  for(i = 0; i < 256; i++){
212  AV_WB24(c->pal+(i*3), palptr[i]);
213  }
214  memcpy(c->work_buf, c->pal, 768);
215  memcpy(c->pal2, palptr, 1024);
216  work_size = 768;
217  }
218  for(i = 0; i < avctx->height; i++){
219  memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
220  src += p->linesize[0];
221  work_size += avctx->width * c->bypp;
222  }
223  }else{
224  int x, y, bh2, bw2, xored;
225  uint8_t *tsrc, *tprev;
226  uint8_t *mv;
227  int mx = 0, my = 0;
228 
229  bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
230  bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
231  mv = c->work_buf + work_size;
232  memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
233  work_size += (bw * bh * 2 + 3) & ~3;
234  /* for now just XOR'ing */
235  for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
236  bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
237  for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
238  bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
239 
240  tsrc = src + x * c->bypp;
241  tprev = prev + x * c->bypp;
242 
243  zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
244  mv[0] = (mx * 2) | !!xored;
245  mv[1] = my * 2;
246  tprev += mx * c->bypp + my * c->pstride;
247  if(xored){
248  for(j = 0; j < bh2; j++){
249  for(i = 0; i < bw2 * c->bypp; i++)
250  c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
251  tsrc += p->linesize[0];
252  tprev += c->pstride;
253  }
254  }
255  }
256  src += p->linesize[0] * ZMBV_BLOCK;
257  prev += c->pstride * ZMBV_BLOCK;
258  }
259  }
260  /* save the previous frame */
261  src = p->data[0];
262  prev = c->prev;
263  for(i = 0; i < avctx->height; i++){
264  memcpy(prev, src, avctx->width * c->bypp);
265  prev += c->pstride;
266  src += p->linesize[0];
267  }
268 
269  if (keyframe)
270  deflateReset(&c->zstream);
271 
272  c->zstream.next_in = c->work_buf;
273  c->zstream.avail_in = work_size;
274  c->zstream.total_in = 0;
275 
276  c->zstream.next_out = c->comp_buf;
277  c->zstream.avail_out = c->comp_size;
278  c->zstream.total_out = 0;
279  if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
280  av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
281  return -1;
282  }
283 
284  pkt_size = c->zstream.total_out + 1 + 6*keyframe;
285  if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
286  return ret;
287  buf = pkt->data;
288 
289  fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
290  *buf++ = fl;
291  if (keyframe) {
292  *buf++ = 0; // hi ver
293  *buf++ = 1; // lo ver
294  *buf++ = 1; // comp
295  *buf++ = c->fmt; // format
296  *buf++ = ZMBV_BLOCK; // block width
297  *buf++ = ZMBV_BLOCK; // block height
298  }
299  memcpy(buf, c->comp_buf, c->zstream.total_out);
300 
301  pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
302  *got_packet = 1;
303 
304  return 0;
305 }
306 
308 {
309  ZmbvEncContext * const c = avctx->priv_data;
310 
311  av_freep(&c->comp_buf);
312  av_freep(&c->work_buf);
313 
314  av_freep(&c->prev_buf);
315  if (c->zlib_init_ok)
316  deflateEnd(&c->zstream);
317 
318  return 0;
319 }
320 
321 /**
322  * Init zmbv encoder
323  */
325 {
326  ZmbvEncContext * const c = avctx->priv_data;
327  int zret; // Zlib return code
328  int i;
329  int lvl = 9;
330  int prev_size, prev_offset;
331 
332  switch (avctx->pix_fmt) {
333  case AV_PIX_FMT_PAL8:
334  c->fmt = ZMBV_FMT_8BPP;
335  c->bypp = 1;
336  break;
337  case AV_PIX_FMT_RGB555LE:
338  c->fmt = ZMBV_FMT_15BPP;
339  c->bypp = 2;
340  break;
341  case AV_PIX_FMT_RGB565LE:
342  c->fmt = ZMBV_FMT_16BPP;
343  c->bypp = 2;
344  break;
345 #ifdef ZMBV_ENABLE_24BPP
346  case AV_PIX_FMT_BGR24:
347  c->fmt = ZMBV_FMT_24BPP;
348  c->bypp = 3;
349  break;
350 #endif //ZMBV_ENABLE_24BPP
351  case AV_PIX_FMT_BGR0:
352  c->fmt = ZMBV_FMT_32BPP;
353  c->bypp = 4;
354  break;
355  default:
356  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
357  return AVERROR(EINVAL);
358  }
359 
360  /* Entropy-based score tables for comparing blocks.
361  * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
362  * Scores are nonnegative, lower is better.
363  */
364  for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
365  c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
366 
367  c->avctx = avctx;
368 
369  c->curfrm = 0;
370  c->keyint = avctx->keyint_min;
371 
372  /* Motion estimation range: maximum distance is -64..63 */
373  c->lrange = c->urange = 8;
374  if(avctx->me_range > 0){
375  c->lrange = FFMIN(avctx->me_range, 64);
376  c->urange = FFMIN(avctx->me_range, 63);
377  }
378 
379  if(avctx->compression_level >= 0)
380  lvl = avctx->compression_level;
381  if(lvl < 0 || lvl > 9){
382  av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
383  return AVERROR(EINVAL);
384  }
385 
386  c->comp_size = avctx->width * c->bypp * avctx->height + 1024 +
387  ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
388  if (!(c->work_buf = av_malloc(c->comp_size))) {
389  av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
390  return AVERROR(ENOMEM);
391  }
392  /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
393  c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
394  ((c->comp_size + 63) >> 6) + 11;
395 
396  /* Allocate compression buffer */
397  if (!(c->comp_buf = av_malloc(c->comp_size))) {
398  av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
399  return AVERROR(ENOMEM);
400  }
401 
402  /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
403  * - The image should be padded with `lrange` rows before and `urange` rows
404  * after.
405  * - The stride should be padded with `lrange` pixels, then rounded up to a
406  * multiple of 16 bytes.
407  * - The first row should also be padded with `lrange` pixels before, then
408  * aligned up to a multiple of 16 bytes.
409  */
410  c->pstride = FFALIGN((avctx->width + c->lrange) * c->bypp, 16);
411  prev_size = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
412  prev_offset = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * c->lrange;
413  if (!(c->prev_buf = av_mallocz(prev_size))) {
414  av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
415  return AVERROR(ENOMEM);
416  }
417  c->prev = c->prev_buf + prev_offset;
418 
419  c->zstream.zalloc = Z_NULL;
420  c->zstream.zfree = Z_NULL;
421  c->zstream.opaque = Z_NULL;
422  zret = deflateInit(&c->zstream, lvl);
423  if (zret != Z_OK) {
424  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
425  return -1;
426  }
427  c->zlib_init_ok = 1;
428 
429  return 0;
430 }
431 
433  .name = "zmbv",
434  .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
435  .type = AVMEDIA_TYPE_VIDEO,
436  .id = AV_CODEC_ID_ZMBV,
437  .priv_data_size = sizeof(ZmbvEncContext),
438  .init = encode_init,
439  .encode2 = encode_frame,
440  .close = encode_end,
441  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
444 #ifdef ZMBV_ENABLE_24BPP
446 #endif //ZMBV_ENABLE_24BPP
448  AV_PIX_FMT_NONE },
449  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
450 };
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
@ AV_CODEC_ID_ZMBV
Definition: codec_id.h:130
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
int32_t bv
Definition: input.c:405
int i
Definition: input.c:407
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
static const int8_t mv[256][2]
Definition: 4xm.c:78
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
#define log2(x)
Definition: libm.h:404
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: macros.h:48
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:106
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
int keyint_min
minimum GOP size
Definition: avcodec.h:1117
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1764
int compression_level
Definition: avcodec.h:608
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:1007
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
uint8_t * data
Definition: packet.h:369
Encoder context.
Definition: zmbvenc.c:63
uint8_t * prev_buf
Definition: zmbvenc.c:70
enum ZmbvFormat fmt
Definition: zmbvenc.c:75
int comp_size
Definition: zmbvenc.c:72
uint8_t * prev
Definition: zmbvenc.c:70
uint8_t * work_buf
Definition: zmbvenc.c:67
uint8_t * comp_buf
Definition: zmbvenc.c:67
uint32_t pal2[256]
Definition: zmbvenc.c:69
AVCodecContext * avctx
Definition: zmbvenc.c:64
z_stream zstream
Definition: zmbvenc.c:77
uint8_t pal[768]
Definition: zmbvenc.c:68
int zlib_init_ok
Definition: zmbvenc.c:76
int score_tab[ZMBV_BLOCK *ZMBV_BLOCK *4+1]
Definition: zmbvenc.c:79
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
AVPacket * pkt
Definition: movenc.c:59
static void deflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:165
static double c[64]
ZmbvFormat
Definition: zmbv.c:41
@ ZMBV_FMT_24BPP
Definition: zmbvenc.c:56
@ ZMBV_FMT_4BPP
Definition: zmbvenc.c:52
@ ZMBV_FMT_32BPP
Definition: zmbvenc.c:57
@ ZMBV_FMT_1BPP
Definition: zmbvenc.c:50
@ ZMBV_FMT_16BPP
Definition: zmbvenc.c:55
@ ZMBV_FMT_2BPP
Definition: zmbvenc.c:51
@ ZMBV_FMT_NONE
Definition: zmbvenc.c:49
@ ZMBV_FMT_8BPP
Definition: zmbvenc.c:53
@ ZMBV_FMT_15BPP
Definition: zmbvenc.c:54
#define ZMBV_KEYFRAME
Definition: zmbvenc.c:38
#define ZMBV_DELTAPAL
Definition: zmbvenc.c:39
#define ZMBV_BLOCK
Definition: zmbvenc.c:45
static av_cold int encode_init(AVCodecContext *avctx)
Init zmbv encoder.
Definition: zmbvenc.c:324
static int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh, int *xored)
Block comparing function XXX should be optimized and moved to DSPContext.
Definition: zmbvenc.c:86
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: zmbvenc.c:167
static av_cold int encode_end(AVCodecContext *avctx)
Definition: zmbvenc.c:307
AVCodec ff_zmbv_encoder
Definition: zmbvenc.c:432
static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride, int x, int y, int *mx, int *my, int *xored)
Motion estimation function TODO make better ME decisions.
Definition: zmbvenc.c:121