You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
9.4KB

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