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.

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