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.

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