FFmpeg  4.2.3
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 {
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;
76  z_stream zstream;
77 
80 
81 
82 /** Block comparing function
83  * XXX should be optimized and moved to DSPContext
84  */
85 static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
86  uint8_t *src2, int stride2, int bw, int bh,
87  int *xored)
88 {
89  int sum = 0;
90  int i, j;
91  uint16_t histogram[256] = {0};
92  int bw_bytes = bw * c->bypp;
93 
94  /* Build frequency histogram of byte values for src[] ^ src2[] */
95  for(j = 0; j < bh; j++){
96  for(i = 0; i < bw_bytes; i++){
97  int t = src[i] ^ src2[i];
98  histogram[t]++;
99  }
100  src += stride;
101  src2 += stride2;
102  }
103 
104  /* If not all the xored values were 0, then the blocks are different */
105  *xored = (histogram[0] < bw_bytes * bh);
106 
107  /* Exit early if blocks are equal */
108  if (!*xored) return 0;
109 
110  /* Sum the entropy of all values */
111  for(i = 0; i < 256; i++)
112  sum += c->score_tab[histogram[i]];
113 
114  return sum;
115 }
116 
117 /** Motion estimation function
118  * TODO make better ME decisions
119  */
120 static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
121  int pstride, int x, int y, int *mx, int *my, int *xored)
122 {
123  int dx, dy, txored, tv, bv, bw, bh;
124  int mx0, my0;
125 
126  mx0 = *mx;
127  my0 = *my;
128  bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
129  bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
130 
131  /* Try (0,0) */
132  bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
133  *mx = *my = 0;
134  if(!bv) return 0;
135 
136  /* Try previous block's MV (if not 0,0) */
137  if (mx0 || my0){
138  tv = block_cmp(c, src, sstride, prev + mx0 * c->bypp + my0 * pstride, pstride, bw, bh, &txored);
139  if(tv < bv){
140  bv = tv;
141  *mx = mx0;
142  *my = my0;
143  *xored = txored;
144  if(!bv) return 0;
145  }
146  }
147 
148  /* Try other MVs from top-to-bottom, left-to-right */
149  for(dy = -c->lrange; dy <= c->urange; dy++){
150  for(dx = -c->lrange; dx <= c->urange; dx++){
151  if(!dx && !dy) continue; // we already tested this block
152  if(dx == mx0 && dy == my0) continue; // this one too
153  tv = block_cmp(c, src, sstride, prev + dx * c->bypp + dy * pstride, pstride, bw, bh, &txored);
154  if(tv < bv){
155  bv = tv;
156  *mx = dx;
157  *my = dy;
158  *xored = txored;
159  if(!bv) return 0;
160  }
161  }
162  }
163  return bv;
164 }
165 
167  const AVFrame *pict, int *got_packet)
168 {
169  ZmbvEncContext * const c = avctx->priv_data;
170  const AVFrame * const p = pict;
171  uint8_t *src, *prev, *buf;
172  uint32_t *palptr;
173  int keyframe, chpal;
174  int fl;
175  int work_size = 0, pkt_size;
176  int bw, bh;
177  int i, j, ret;
178 
179  keyframe = !c->curfrm;
180  c->curfrm++;
181  if(c->curfrm == c->keyint)
182  c->curfrm = 0;
183 #if FF_API_CODED_FRAME
186  avctx->coded_frame->key_frame = keyframe;
188 #endif
189 
190  palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
191  chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
192 
193  src = p->data[0];
194  prev = c->prev;
195  if(chpal){
196  uint8_t tpal[3];
197  for(i = 0; i < 256; i++){
198  AV_WB24(tpal, palptr[i]);
199  c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
200  c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
201  c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
202  c->pal[i * 3 + 0] = tpal[0];
203  c->pal[i * 3 + 1] = tpal[1];
204  c->pal[i * 3 + 2] = tpal[2];
205  }
206  memcpy(c->pal2, palptr, 1024);
207  }
208  if(keyframe){
209  if (palptr){
210  for(i = 0; i < 256; i++){
211  AV_WB24(c->pal+(i*3), palptr[i]);
212  }
213  memcpy(c->work_buf, c->pal, 768);
214  memcpy(c->pal2, palptr, 1024);
215  work_size = 768;
216  }
217  for(i = 0; i < avctx->height; i++){
218  memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
219  src += p->linesize[0];
220  work_size += avctx->width * c->bypp;
221  }
222  }else{
223  int x, y, bh2, bw2, xored;
224  uint8_t *tsrc, *tprev;
225  uint8_t *mv;
226  int mx = 0, my = 0;
227 
228  bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
229  bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
230  mv = c->work_buf + work_size;
231  memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
232  work_size += (bw * bh * 2 + 3) & ~3;
233  /* for now just XOR'ing */
234  for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
235  bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
236  for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
237  bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
238 
239  tsrc = src + x * c->bypp;
240  tprev = prev + x * c->bypp;
241 
242  zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
243  mv[0] = (mx << 1) | !!xored;
244  mv[1] = my << 1;
245  tprev += mx * c->bypp + my * c->pstride;
246  if(xored){
247  for(j = 0; j < bh2; j++){
248  for(i = 0; i < bw2 * c->bypp; i++)
249  c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
250  tsrc += p->linesize[0];
251  tprev += c->pstride;
252  }
253  }
254  }
255  src += p->linesize[0] * ZMBV_BLOCK;
256  prev += c->pstride * ZMBV_BLOCK;
257  }
258  }
259  /* save the previous frame */
260  src = p->data[0];
261  prev = c->prev;
262  for(i = 0; i < avctx->height; i++){
263  memcpy(prev, src, avctx->width * c->bypp);
264  prev += c->pstride;
265  src += p->linesize[0];
266  }
267 
268  if (keyframe)
269  deflateReset(&c->zstream);
270 
271  c->zstream.next_in = c->work_buf;
272  c->zstream.avail_in = work_size;
273  c->zstream.total_in = 0;
274 
275  c->zstream.next_out = c->comp_buf;
276  c->zstream.avail_out = c->comp_size;
277  c->zstream.total_out = 0;
278  if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
279  av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
280  return -1;
281  }
282 
283  pkt_size = c->zstream.total_out + 1 + 6*keyframe;
284  if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
285  return ret;
286  buf = pkt->data;
287 
288  fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
289  *buf++ = fl;
290  if (keyframe) {
291  *buf++ = 0; // hi ver
292  *buf++ = 1; // lo ver
293  *buf++ = 1; // comp
294  *buf++ = c->fmt; // format
295  *buf++ = ZMBV_BLOCK; // block width
296  *buf++ = ZMBV_BLOCK; // block height
297  }
298  memcpy(buf, c->comp_buf, c->zstream.total_out);
299 
300  pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
301  *got_packet = 1;
302 
303  return 0;
304 }
305 
307 {
308  ZmbvEncContext * const c = avctx->priv_data;
309 
310  av_freep(&c->comp_buf);
311  av_freep(&c->work_buf);
312 
313  deflateEnd(&c->zstream);
314  av_freep(&c->prev_buf);
315 
316  return 0;
317 }
318 
319 /**
320  * Init zmbv encoder
321  */
323 {
324  ZmbvEncContext * const c = avctx->priv_data;
325  int zret; // Zlib return code
326  int i;
327  int lvl = 9;
328  int prev_size, prev_offset;
329 
330  switch (avctx->pix_fmt) {
331  case AV_PIX_FMT_PAL8:
332  c->fmt = ZMBV_FMT_8BPP;
333  c->bypp = 1;
334  break;
335  case AV_PIX_FMT_RGB555LE:
336  c->fmt = ZMBV_FMT_15BPP;
337  c->bypp = 2;
338  break;
339  case AV_PIX_FMT_RGB565LE:
340  c->fmt = ZMBV_FMT_16BPP;
341  c->bypp = 2;
342  break;
343 #ifdef ZMBV_ENABLE_24BPP
344  case AV_PIX_FMT_BGR24:
345  c->fmt = ZMBV_FMT_24BPP;
346  c->bypp = 3;
347  break;
348 #endif //ZMBV_ENABLE_24BPP
349  case AV_PIX_FMT_BGR0:
350  c->fmt = ZMBV_FMT_32BPP;
351  c->bypp = 4;
352  break;
353  default:
354  av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
355  return AVERROR(EINVAL);
356  }
357 
358  /* Entropy-based score tables for comparing blocks.
359  * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
360  * Scores are nonnegative, lower is better.
361  */
362  for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
363  c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
364 
365  c->avctx = avctx;
366 
367  c->curfrm = 0;
368  c->keyint = avctx->keyint_min;
369 
370  /* Motion estimation range: maximum distance is -64..63 */
371  c->lrange = c->urange = 8;
372  if(avctx->me_range > 0){
373  c->lrange = FFMIN(avctx->me_range, 64);
374  c->urange = FFMIN(avctx->me_range, 63);
375  }
376 
377  if(avctx->compression_level >= 0)
378  lvl = avctx->compression_level;
379  if(lvl < 0 || lvl > 9){
380  av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
381  return AVERROR(EINVAL);
382  }
383 
384  // Needed if zlib unused or init aborted before deflateInit
385  memset(&c->zstream, 0, sizeof(z_stream));
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 
428  return 0;
429 }
430 
432  .name = "zmbv",
433  .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
434  .type = AVMEDIA_TYPE_VIDEO,
435  .id = AV_CODEC_ID_ZMBV,
436  .priv_data_size = sizeof(ZmbvEncContext),
437  .init = encode_init,
438  .encode2 = encode_frame,
439  .close = encode_end,
440  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
443 #ifdef ZMBV_ENABLE_24BPP
445 #endif //ZMBV_ENABLE_24BPP
447  AV_PIX_FMT_NONE },
448 };
uint8_t pal[768]
Definition: zmbvenc.c:68
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int comp_size
Definition: zmbvenc.c:72
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:108
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
uint8_t * comp_buf
Definition: zmbvenc.c:67
static av_cold int encode_end(AVCodecContext *avctx)
Definition: zmbvenc.c:306
static AVPacket pkt
#define src
Definition: vp8dsp.c:254
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3481
#define log2(x)
Definition: libm.h:404
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:106
#define ZMBV_KEYFRAME
Definition: zmbvenc.c:38
uint8_t * prev
Definition: zmbvenc.c:70
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:32
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
Definition: avcodec.h:2036
Encoder context.
Definition: zmbvenc.c:63
uint8_t * data
Definition: avcodec.h:1477
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
AVCodec ff_zmbv_encoder
Definition: zmbvenc.c:431
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t * work_buf
Definition: zmbvenc.c:67
static av_cold int encode_init(AVCodecContext *avctx)
Init zmbv encoder.
Definition: zmbvenc.c:322
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
ZmbvFormat
Definition: zmbv.c:41
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
#define FFMIN(a, b)
Definition: common.h:96
int width
picture width / height.
Definition: avcodec.h:1738
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
AVCodecContext * avctx
Definition: zmbvenc.c:64
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
static const int8_t mv[256][2]
Definition: 4xm.c:77
#define ZMBV_BLOCK
Definition: zmbvenc.c:45
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
enum ZmbvFormat fmt
Definition: zmbvenc.c:75
#define ZMBV_DELTAPAL
Definition: zmbvenc.c:39
Libavcodec external API header.
int score_tab[ZMBV_BLOCK *ZMBV_BLOCK *4+1]
Definition: zmbvenc.c:78
int compression_level
Definition: avcodec.h:1637
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
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:120
main external API structure.
Definition: avcodec.h:1565
void * buf
Definition: avisynth_c.h:766
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: zmbvenc.c:166
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
uint8_t * prev_buf
Definition: zmbvenc.c:70
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
common internal and external API header
static double c[64]
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2815
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:164
void * priv_data
Definition: avcodec.h:1592
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
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:85
#define av_freep(p)
z_stream zstream
Definition: zmbvenc.c:76
uint32_t pal2[256]
Definition: zmbvenc.c:69
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1454
Predicted.
Definition: avutil.h:275
int keyint_min
minimum GOP size
Definition: avcodec.h:2146