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.

328 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 "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. /** Block comparing function
  50. * XXX should be optimized and moved to DSPContext
  51. * TODO handle out of edge ME
  52. */
  53. static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2, int bw, int bh)
  54. {
  55. int sum = 0;
  56. int i, j;
  57. for(j = 0; j < bh; j++){
  58. for(i = 0; i < bw; i++)
  59. sum += src[i] ^ src2[i];
  60. src += stride;
  61. src2 += stride2;
  62. }
  63. return sum;
  64. }
  65. /** Motion estimation function
  66. * TODO make better ME decisions
  67. */
  68. static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev, int pstride,
  69. int x, int y, int *mx, int *my)
  70. {
  71. int dx, dy, tx, ty, tv, bv, bw, bh;
  72. *mx = *my = 0;
  73. bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
  74. bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
  75. bv = block_cmp(src, sstride, prev, pstride, bw, bh);
  76. if(!bv) return 0;
  77. for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
  78. for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
  79. if(tx == x && ty == y) continue; // we already tested this block
  80. dx = tx - x;
  81. dy = ty - y;
  82. tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh);
  83. if(tv < bv){
  84. bv = tv;
  85. *mx = dx;
  86. *my = dy;
  87. if(!bv) return 0;
  88. }
  89. }
  90. }
  91. return bv;
  92. }
  93. static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  94. {
  95. ZmbvEncContext * const c = avctx->priv_data;
  96. AVFrame *pict = data;
  97. AVFrame * const p = &c->pic;
  98. uint8_t *src, *prev;
  99. uint32_t *palptr;
  100. int zret = Z_OK;
  101. int len = 0;
  102. int keyframe, chpal;
  103. int fl;
  104. int work_size = 0;
  105. int bw, bh;
  106. int i, j;
  107. keyframe = !c->curfrm;
  108. c->curfrm++;
  109. if(c->curfrm == c->keyint)
  110. c->curfrm = 0;
  111. *p = *pict;
  112. p->pict_type= keyframe ? FF_I_TYPE : FF_P_TYPE;
  113. p->key_frame= keyframe;
  114. chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
  115. fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
  116. *buf++ = fl; len++;
  117. if(keyframe){
  118. deflateReset(&c->zstream);
  119. *buf++ = 0; len++; // hi ver
  120. *buf++ = 1; len++; // lo ver
  121. *buf++ = 1; len++; // comp
  122. *buf++ = 4; len++; // format - 8bpp
  123. *buf++ = ZMBV_BLOCK; len++; // block width
  124. *buf++ = ZMBV_BLOCK; len++; // block height
  125. }
  126. palptr = (uint32_t*)p->data[1];
  127. src = p->data[0];
  128. prev = c->prev;
  129. if(chpal){
  130. uint8_t tpal[3];
  131. for(i = 0; i < 256; i++){
  132. tpal[0] = palptr[i] >> 16;
  133. tpal[1] = palptr[i] >> 8;
  134. tpal[2] = palptr[i];
  135. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  136. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  137. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  138. c->pal[i * 3 + 0] = tpal[0];
  139. c->pal[i * 3 + 1] = tpal[1];
  140. c->pal[i * 3 + 2] = tpal[2];
  141. }
  142. memcpy(c->pal2, p->data[1], 1024);
  143. }
  144. if(keyframe){
  145. for(i = 0; i < 256; i++){
  146. c->pal[i*3 + 0] = palptr[i] >> 16;
  147. c->pal[i*3 + 1] = palptr[i] >> 8;
  148. c->pal[i*3 + 2] = 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 int encode_init(AVCodecContext *avctx)
  217. {
  218. ZmbvEncContext * const c = avctx->priv_data;
  219. int zret; // Zlib return code
  220. int lvl = 9;
  221. c->avctx = avctx;
  222. c->pic.data[0] = NULL;
  223. c->curfrm = 0;
  224. c->keyint = avctx->keyint_min;
  225. c->range = 8;
  226. if(avctx->me_range > 0)
  227. c->range = FFMIN(avctx->me_range, 127);
  228. if(avctx->compression_level >= 0)
  229. lvl = avctx->compression_level;
  230. if(lvl < 0 || lvl > 9){
  231. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  232. return -1;
  233. }
  234. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  235. return -1;
  236. }
  237. // Needed if zlib unused or init aborted before deflateInit
  238. memset(&(c->zstream), 0, sizeof(z_stream));
  239. c->comp_size = avctx->width * avctx->height + 1024 +
  240. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  241. if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
  242. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  243. return -1;
  244. }
  245. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  246. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  247. ((c->comp_size + 63) >> 6) + 11;
  248. /* Allocate compression buffer */
  249. if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
  250. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  251. return -1;
  252. }
  253. c->pstride = (avctx->width + 15) & ~15;
  254. if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
  255. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  256. return -1;
  257. }
  258. c->zstream.zalloc = Z_NULL;
  259. c->zstream.zfree = Z_NULL;
  260. c->zstream.opaque = Z_NULL;
  261. zret = deflateInit(&(c->zstream), lvl);
  262. if (zret != Z_OK) {
  263. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  264. return -1;
  265. }
  266. return 0;
  267. }
  268. /**
  269. * Uninit zmbv encoder
  270. */
  271. static int encode_end(AVCodecContext *avctx)
  272. {
  273. ZmbvEncContext * const c = avctx->priv_data;
  274. av_freep(&c->comp_buf);
  275. av_freep(&c->work_buf);
  276. deflateEnd(&(c->zstream));
  277. av_freep(&c->prev);
  278. return 0;
  279. }
  280. AVCodec zmbv_encoder = {
  281. "zmbv",
  282. CODEC_TYPE_VIDEO,
  283. CODEC_ID_ZMBV,
  284. sizeof(ZmbvEncContext),
  285. encode_init,
  286. encode_frame,
  287. encode_end,
  288. .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},
  289. };