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.

324 lines
9.2KB

  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. AV_WB24(tpal, palptr[i]);
  133. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  134. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  135. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  136. c->pal[i * 3 + 0] = tpal[0];
  137. c->pal[i * 3 + 1] = tpal[1];
  138. c->pal[i * 3 + 2] = tpal[2];
  139. }
  140. memcpy(c->pal2, p->data[1], 1024);
  141. }
  142. if(keyframe){
  143. for(i = 0; i < 256; i++){
  144. AV_WB24(c->pal+(i*3), palptr[i]);
  145. }
  146. memcpy(c->work_buf, c->pal, 768);
  147. memcpy(c->pal2, p->data[1], 1024);
  148. work_size = 768;
  149. for(i = 0; i < avctx->height; i++){
  150. memcpy(c->work_buf + work_size, src, avctx->width);
  151. src += p->linesize[0];
  152. work_size += avctx->width;
  153. }
  154. }else{
  155. int x, y, bh2, bw2;
  156. uint8_t *tsrc, *tprev;
  157. uint8_t *mv;
  158. int mx, my, bv;
  159. bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  160. bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  161. mv = c->work_buf + work_size;
  162. memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
  163. work_size += (bw * bh * 2 + 3) & ~3;
  164. /* for now just XOR'ing */
  165. for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
  166. bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
  167. for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
  168. bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
  169. tsrc = src + x;
  170. tprev = prev + x;
  171. bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my);
  172. mv[0] = (mx << 1) | !!bv;
  173. mv[1] = my << 1;
  174. tprev += mx + my * c->pstride;
  175. if(bv){
  176. for(j = 0; j < bh2; j++){
  177. for(i = 0; i < bw2; i++)
  178. c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
  179. tsrc += p->linesize[0];
  180. tprev += c->pstride;
  181. }
  182. }
  183. }
  184. src += p->linesize[0] * ZMBV_BLOCK;
  185. prev += c->pstride * ZMBV_BLOCK;
  186. }
  187. }
  188. /* save the previous frame */
  189. src = p->data[0];
  190. prev = c->prev;
  191. for(i = 0; i < avctx->height; i++){
  192. memcpy(prev, src, avctx->width);
  193. prev += c->pstride;
  194. src += p->linesize[0];
  195. }
  196. c->zstream.next_in = c->work_buf;
  197. c->zstream.avail_in = work_size;
  198. c->zstream.total_in = 0;
  199. c->zstream.next_out = c->comp_buf;
  200. c->zstream.avail_out = c->comp_size;
  201. c->zstream.total_out = 0;
  202. if((zret = deflate(&c->zstream, Z_SYNC_FLUSH)) != Z_OK){
  203. av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
  204. return -1;
  205. }
  206. memcpy(buf, c->comp_buf, c->zstream.total_out);
  207. return len + c->zstream.total_out;
  208. }
  209. /**
  210. * Init zmbv encoder
  211. */
  212. static int encode_init(AVCodecContext *avctx)
  213. {
  214. ZmbvEncContext * const c = avctx->priv_data;
  215. int zret; // Zlib return code
  216. int lvl = 9;
  217. c->avctx = avctx;
  218. c->pic.data[0] = NULL;
  219. c->curfrm = 0;
  220. c->keyint = avctx->keyint_min;
  221. c->range = 8;
  222. if(avctx->me_range > 0)
  223. c->range = FFMIN(avctx->me_range, 127);
  224. if(avctx->compression_level >= 0)
  225. lvl = avctx->compression_level;
  226. if(lvl < 0 || lvl > 9){
  227. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  228. return -1;
  229. }
  230. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  231. return -1;
  232. }
  233. // Needed if zlib unused or init aborted before deflateInit
  234. memset(&(c->zstream), 0, sizeof(z_stream));
  235. c->comp_size = avctx->width * avctx->height + 1024 +
  236. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  237. if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
  238. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  239. return -1;
  240. }
  241. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  242. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  243. ((c->comp_size + 63) >> 6) + 11;
  244. /* Allocate compression buffer */
  245. if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
  246. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  247. return -1;
  248. }
  249. c->pstride = (avctx->width + 15) & ~15;
  250. if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
  251. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  252. return -1;
  253. }
  254. c->zstream.zalloc = Z_NULL;
  255. c->zstream.zfree = Z_NULL;
  256. c->zstream.opaque = Z_NULL;
  257. zret = deflateInit(&(c->zstream), lvl);
  258. if (zret != Z_OK) {
  259. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. /**
  265. * Uninit zmbv encoder
  266. */
  267. static int encode_end(AVCodecContext *avctx)
  268. {
  269. ZmbvEncContext * const c = avctx->priv_data;
  270. av_freep(&c->comp_buf);
  271. av_freep(&c->work_buf);
  272. deflateEnd(&(c->zstream));
  273. av_freep(&c->prev);
  274. return 0;
  275. }
  276. AVCodec zmbv_encoder = {
  277. "zmbv",
  278. CODEC_TYPE_VIDEO,
  279. CODEC_ID_ZMBV,
  280. sizeof(ZmbvEncContext),
  281. encode_init,
  282. encode_frame,
  283. encode_end,
  284. .pix_fmts = (enum PixelFormat[]){PIX_FMT_PAL8, -1},
  285. };