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.

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