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.

350 lines
10KB

  1. /*
  2. * Zip Motion Blocks Video (ZMBV) encoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Zip Motion Blocks Video encoder
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include "internal.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. static int score_tab[256];
  51. /** Block comparing function
  52. * XXX should be optimized and moved to DSPContext
  53. * TODO handle out of edge ME
  54. */
  55. static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2,
  56. int bw, int bh, int *xored)
  57. {
  58. int sum = 0;
  59. int i, j;
  60. uint8_t histogram[256] = {0};
  61. *xored = 0;
  62. for(j = 0; j < bh; j++){
  63. for(i = 0; i < bw; i++){
  64. int t = src[i] ^ src2[i];
  65. histogram[t]++;
  66. *xored |= t;
  67. }
  68. src += stride;
  69. src2 += stride2;
  70. }
  71. for(i = 1; i < 256; i++)
  72. sum += score_tab[histogram[i]];
  73. return sum;
  74. }
  75. /** Motion estimation function
  76. * TODO make better ME decisions
  77. */
  78. static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
  79. int pstride, int x, int y, int *mx, int *my, int *xored)
  80. {
  81. int dx, dy, tx, ty, tv, bv, bw, bh;
  82. *mx = *my = 0;
  83. bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
  84. bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
  85. bv = block_cmp(src, sstride, prev, pstride, bw, bh, xored);
  86. if(!bv) return 0;
  87. for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
  88. for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
  89. if(tx == x && ty == y) continue; // we already tested this block
  90. dx = tx - x;
  91. dy = ty - y;
  92. tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh, xored);
  93. if(tv < bv){
  94. bv = tv;
  95. *mx = dx;
  96. *my = dy;
  97. if(!bv) return 0;
  98. }
  99. }
  100. }
  101. return bv;
  102. }
  103. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  104. const AVFrame *pict, int *got_packet)
  105. {
  106. ZmbvEncContext * const c = avctx->priv_data;
  107. AVFrame * const p = &c->pic;
  108. uint8_t *src, *prev, *buf;
  109. uint32_t *palptr;
  110. int keyframe, chpal;
  111. int fl;
  112. int work_size = 0, pkt_size;
  113. int bw, bh;
  114. int i, j, ret;
  115. keyframe = !c->curfrm;
  116. c->curfrm++;
  117. if(c->curfrm == c->keyint)
  118. c->curfrm = 0;
  119. *p = *pict;
  120. p->pict_type= keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  121. p->key_frame= keyframe;
  122. chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
  123. palptr = (uint32_t*)p->data[1];
  124. src = p->data[0];
  125. prev = c->prev;
  126. if(chpal){
  127. uint8_t tpal[3];
  128. for(i = 0; i < 256; i++){
  129. AV_WB24(tpal, palptr[i]);
  130. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  131. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  132. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  133. c->pal[i * 3 + 0] = tpal[0];
  134. c->pal[i * 3 + 1] = tpal[1];
  135. c->pal[i * 3 + 2] = tpal[2];
  136. }
  137. memcpy(c->pal2, p->data[1], 1024);
  138. }
  139. if(keyframe){
  140. for(i = 0; i < 256; i++){
  141. AV_WB24(c->pal+(i*3), palptr[i]);
  142. }
  143. memcpy(c->work_buf, c->pal, 768);
  144. memcpy(c->pal2, p->data[1], 1024);
  145. work_size = 768;
  146. for(i = 0; i < avctx->height; i++){
  147. memcpy(c->work_buf + work_size, src, avctx->width);
  148. src += p->linesize[0];
  149. work_size += avctx->width;
  150. }
  151. }else{
  152. int x, y, bh2, bw2, xored;
  153. uint8_t *tsrc, *tprev;
  154. uint8_t *mv;
  155. int mx, my;
  156. bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  157. bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  158. mv = c->work_buf + work_size;
  159. memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
  160. work_size += (bw * bh * 2 + 3) & ~3;
  161. /* for now just XOR'ing */
  162. for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
  163. bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
  164. for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
  165. bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
  166. tsrc = src + x;
  167. tprev = prev + x;
  168. zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
  169. mv[0] = (mx << 1) | !!xored;
  170. mv[1] = my << 1;
  171. tprev += mx + my * c->pstride;
  172. if(xored){
  173. for(j = 0; j < bh2; j++){
  174. for(i = 0; i < bw2; i++)
  175. c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
  176. tsrc += p->linesize[0];
  177. tprev += c->pstride;
  178. }
  179. }
  180. }
  181. src += p->linesize[0] * ZMBV_BLOCK;
  182. prev += c->pstride * ZMBV_BLOCK;
  183. }
  184. }
  185. /* save the previous frame */
  186. src = p->data[0];
  187. prev = c->prev;
  188. for(i = 0; i < avctx->height; i++){
  189. memcpy(prev, src, avctx->width);
  190. prev += c->pstride;
  191. src += p->linesize[0];
  192. }
  193. if (keyframe)
  194. deflateReset(&c->zstream);
  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(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
  202. av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
  203. return -1;
  204. }
  205. pkt_size = c->zstream.total_out + 1 + 6*keyframe;
  206. if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
  207. av_log(avctx, AV_LOG_ERROR, "Error getting packet of size %d.\n", pkt_size);
  208. return ret;
  209. }
  210. buf = pkt->data;
  211. fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
  212. *buf++ = fl;
  213. if (keyframe) {
  214. *buf++ = 0; // hi ver
  215. *buf++ = 1; // lo ver
  216. *buf++ = 1; // comp
  217. *buf++ = 4; // format - 8bpp
  218. *buf++ = ZMBV_BLOCK; // block width
  219. *buf++ = ZMBV_BLOCK; // block height
  220. }
  221. memcpy(buf, c->comp_buf, c->zstream.total_out);
  222. pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
  223. *got_packet = 1;
  224. return 0;
  225. }
  226. /**
  227. * Init zmbv encoder
  228. */
  229. static av_cold int encode_init(AVCodecContext *avctx)
  230. {
  231. ZmbvEncContext * const c = avctx->priv_data;
  232. int zret; // Zlib return code
  233. int i;
  234. int lvl = 9;
  235. for(i=1; i<256; i++)
  236. score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
  237. c->avctx = avctx;
  238. c->curfrm = 0;
  239. c->keyint = avctx->keyint_min;
  240. c->range = 8;
  241. if(avctx->me_range > 0)
  242. c->range = FFMIN(avctx->me_range, 127);
  243. if(avctx->compression_level >= 0)
  244. lvl = avctx->compression_level;
  245. if(lvl < 0 || lvl > 9){
  246. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  247. return AVERROR(EINVAL);
  248. }
  249. // Needed if zlib unused or init aborted before deflateInit
  250. memset(&c->zstream, 0, sizeof(z_stream));
  251. c->comp_size = avctx->width * avctx->height + 1024 +
  252. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  253. if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
  254. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  255. return AVERROR(ENOMEM);
  256. }
  257. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  258. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  259. ((c->comp_size + 63) >> 6) + 11;
  260. /* Allocate compression buffer */
  261. if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
  262. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  263. return AVERROR(ENOMEM);
  264. }
  265. c->pstride = FFALIGN(avctx->width, 16);
  266. if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
  267. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  268. return AVERROR(ENOMEM);
  269. }
  270. c->zstream.zalloc = Z_NULL;
  271. c->zstream.zfree = Z_NULL;
  272. c->zstream.opaque = Z_NULL;
  273. zret = deflateInit(&c->zstream, lvl);
  274. if (zret != Z_OK) {
  275. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  276. return -1;
  277. }
  278. avctx->coded_frame = &c->pic;
  279. return 0;
  280. }
  281. /**
  282. * Uninit zmbv encoder
  283. */
  284. static av_cold int encode_end(AVCodecContext *avctx)
  285. {
  286. ZmbvEncContext * const c = avctx->priv_data;
  287. av_freep(&c->comp_buf);
  288. av_freep(&c->work_buf);
  289. deflateEnd(&c->zstream);
  290. av_freep(&c->prev);
  291. return 0;
  292. }
  293. AVCodec ff_zmbv_encoder = {
  294. .name = "zmbv",
  295. .type = AVMEDIA_TYPE_VIDEO,
  296. .id = CODEC_ID_ZMBV,
  297. .priv_data_size = sizeof(ZmbvEncContext),
  298. .init = encode_init,
  299. .encode2 = encode_frame,
  300. .close = encode_end,
  301. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_PAL8, PIX_FMT_NONE },
  302. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  303. };