zmbvenc.c 9.7 KB

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