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.

355 lines
11KB

  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
  23. * Zip Motion Blocks Video encoder
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. #include <zlib.h>
  32. #define ZMBV_KEYFRAME 1
  33. #define ZMBV_DELTAPAL 2
  34. #define ZMBV_BLOCK 16
  35. /**
  36. * Encoder context
  37. */
  38. typedef struct ZmbvEncContext {
  39. AVCodecContext *avctx;
  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. int score_tab[ZMBV_BLOCK * ZMBV_BLOCK + 1];
  50. } ZmbvEncContext;
  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(ZmbvEncContext *c, uint8_t *src, int stride,
  56. uint8_t *src2, int stride2, int bw, int bh,
  57. int *xored)
  58. {
  59. int sum = 0;
  60. int i, j;
  61. uint16_t histogram[256] = {0};
  62. /* Build frequency histogram of byte values for src[] ^ src2[] */
  63. for(j = 0; j < bh; j++){
  64. for(i = 0; i < bw; i++){
  65. int t = src[i] ^ src2[i];
  66. histogram[t]++;
  67. }
  68. src += stride;
  69. src2 += stride2;
  70. }
  71. /* If not all the xored values were 0, then the blocks are different */
  72. *xored = (histogram[0] < bw * bh);
  73. /* Exit early if blocks are equal */
  74. if (!*xored) return 0;
  75. /* Sum the entropy of all values */
  76. for(i = 0; i < 256; i++)
  77. sum += c->score_tab[histogram[i]];
  78. return sum;
  79. }
  80. /** Motion estimation function
  81. * TODO make better ME decisions
  82. */
  83. static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
  84. int pstride, int x, int y, int *mx, int *my, int *xored)
  85. {
  86. int dx, dy, tx, ty, tv, bv, bw, bh;
  87. *mx = *my = 0;
  88. bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
  89. bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
  90. bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
  91. if(!bv) return 0;
  92. for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
  93. for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
  94. if(tx == x && ty == y) continue; // we already tested this block
  95. dx = tx - x;
  96. dy = ty - y;
  97. tv = block_cmp(c, src, sstride, prev + dx + dy * pstride, pstride, bw, bh, xored);
  98. if(tv < bv){
  99. bv = tv;
  100. *mx = dx;
  101. *my = dy;
  102. if(!bv) return 0;
  103. }
  104. }
  105. }
  106. return bv;
  107. }
  108. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  109. const AVFrame *pict, int *got_packet)
  110. {
  111. ZmbvEncContext * const c = avctx->priv_data;
  112. const AVFrame * const p = pict;
  113. uint8_t *src, *prev, *buf;
  114. uint32_t *palptr;
  115. int keyframe, chpal;
  116. int fl;
  117. int work_size = 0, pkt_size;
  118. int bw, bh;
  119. int i, j, ret;
  120. keyframe = !c->curfrm;
  121. c->curfrm++;
  122. if(c->curfrm == c->keyint)
  123. c->curfrm = 0;
  124. #if FF_API_CODED_FRAME
  125. FF_DISABLE_DEPRECATION_WARNINGS
  126. avctx->coded_frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  127. avctx->coded_frame->key_frame = keyframe;
  128. FF_ENABLE_DEPRECATION_WARNINGS
  129. #endif
  130. chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
  131. palptr = (uint32_t*)p->data[1];
  132. src = p->data[0];
  133. prev = c->prev;
  134. if(chpal){
  135. uint8_t tpal[3];
  136. for(i = 0; i < 256; i++){
  137. AV_WB24(tpal, palptr[i]);
  138. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  139. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  140. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  141. c->pal[i * 3 + 0] = tpal[0];
  142. c->pal[i * 3 + 1] = tpal[1];
  143. c->pal[i * 3 + 2] = tpal[2];
  144. }
  145. memcpy(c->pal2, p->data[1], 1024);
  146. }
  147. if(keyframe){
  148. for(i = 0; i < 256; i++){
  149. AV_WB24(c->pal+(i*3), palptr[i]);
  150. }
  151. memcpy(c->work_buf, c->pal, 768);
  152. memcpy(c->pal2, p->data[1], 1024);
  153. work_size = 768;
  154. for(i = 0; i < avctx->height; i++){
  155. memcpy(c->work_buf + work_size, src, avctx->width);
  156. src += p->linesize[0];
  157. work_size += avctx->width;
  158. }
  159. }else{
  160. int x, y, bh2, bw2, xored;
  161. uint8_t *tsrc, *tprev;
  162. uint8_t *mv;
  163. int mx, my;
  164. bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  165. bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  166. mv = c->work_buf + work_size;
  167. memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
  168. work_size += (bw * bh * 2 + 3) & ~3;
  169. /* for now just XOR'ing */
  170. for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
  171. bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
  172. for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
  173. bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
  174. tsrc = src + x;
  175. tprev = prev + x;
  176. zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
  177. mv[0] = (mx << 1) | !!xored;
  178. mv[1] = my << 1;
  179. tprev += mx + my * c->pstride;
  180. if(xored){
  181. for(j = 0; j < bh2; j++){
  182. for(i = 0; i < bw2; i++)
  183. c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
  184. tsrc += p->linesize[0];
  185. tprev += c->pstride;
  186. }
  187. }
  188. }
  189. src += p->linesize[0] * ZMBV_BLOCK;
  190. prev += c->pstride * ZMBV_BLOCK;
  191. }
  192. }
  193. /* save the previous frame */
  194. src = p->data[0];
  195. prev = c->prev;
  196. for(i = 0; i < avctx->height; i++){
  197. memcpy(prev, src, avctx->width);
  198. prev += c->pstride;
  199. src += p->linesize[0];
  200. }
  201. if (keyframe)
  202. deflateReset(&c->zstream);
  203. c->zstream.next_in = c->work_buf;
  204. c->zstream.avail_in = work_size;
  205. c->zstream.total_in = 0;
  206. c->zstream.next_out = c->comp_buf;
  207. c->zstream.avail_out = c->comp_size;
  208. c->zstream.total_out = 0;
  209. if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
  210. av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
  211. return -1;
  212. }
  213. pkt_size = c->zstream.total_out + 1 + 6*keyframe;
  214. if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
  215. return ret;
  216. buf = pkt->data;
  217. fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
  218. *buf++ = fl;
  219. if (keyframe) {
  220. *buf++ = 0; // hi ver
  221. *buf++ = 1; // lo ver
  222. *buf++ = 1; // comp
  223. *buf++ = 4; // format - 8bpp
  224. *buf++ = ZMBV_BLOCK; // block width
  225. *buf++ = ZMBV_BLOCK; // block height
  226. }
  227. memcpy(buf, c->comp_buf, c->zstream.total_out);
  228. pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
  229. *got_packet = 1;
  230. return 0;
  231. }
  232. static av_cold int encode_end(AVCodecContext *avctx)
  233. {
  234. ZmbvEncContext * const c = avctx->priv_data;
  235. av_freep(&c->comp_buf);
  236. av_freep(&c->work_buf);
  237. deflateEnd(&c->zstream);
  238. av_freep(&c->prev);
  239. return 0;
  240. }
  241. /**
  242. * Init zmbv encoder
  243. */
  244. static av_cold int encode_init(AVCodecContext *avctx)
  245. {
  246. ZmbvEncContext * const c = avctx->priv_data;
  247. int zret; // Zlib return code
  248. int i;
  249. int lvl = 9;
  250. /* Entropy-based score tables for comparing blocks.
  251. * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
  252. * Scores are nonnegative, lower is better.
  253. */
  254. for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK; i++)
  255. c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK)) * 256;
  256. c->avctx = avctx;
  257. c->curfrm = 0;
  258. c->keyint = avctx->keyint_min;
  259. c->range = 8;
  260. if(avctx->me_range > 0)
  261. c->range = FFMIN(avctx->me_range, 127);
  262. if(avctx->compression_level >= 0)
  263. lvl = avctx->compression_level;
  264. if(lvl < 0 || lvl > 9){
  265. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  266. return AVERROR(EINVAL);
  267. }
  268. // Needed if zlib unused or init aborted before deflateInit
  269. memset(&c->zstream, 0, sizeof(z_stream));
  270. c->comp_size = avctx->width * avctx->height + 1024 +
  271. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  272. if (!(c->work_buf = av_malloc(c->comp_size))) {
  273. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  274. return AVERROR(ENOMEM);
  275. }
  276. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  277. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  278. ((c->comp_size + 63) >> 6) + 11;
  279. /* Allocate compression buffer */
  280. if (!(c->comp_buf = av_malloc(c->comp_size))) {
  281. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  282. return AVERROR(ENOMEM);
  283. }
  284. c->pstride = FFALIGN(avctx->width, 16);
  285. if (!(c->prev = av_malloc(c->pstride * avctx->height))) {
  286. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  287. return AVERROR(ENOMEM);
  288. }
  289. c->zstream.zalloc = Z_NULL;
  290. c->zstream.zfree = Z_NULL;
  291. c->zstream.opaque = Z_NULL;
  292. zret = deflateInit(&c->zstream, lvl);
  293. if (zret != Z_OK) {
  294. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  295. return -1;
  296. }
  297. return 0;
  298. }
  299. AVCodec ff_zmbv_encoder = {
  300. .name = "zmbv",
  301. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  302. .type = AVMEDIA_TYPE_VIDEO,
  303. .id = AV_CODEC_ID_ZMBV,
  304. .priv_data_size = sizeof(ZmbvEncContext),
  305. .init = encode_init,
  306. .encode2 = encode_frame,
  307. .close = encode_end,
  308. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE },
  309. };