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.

335 lines
9.6KB

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