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.

349 lines
10KB

  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. AVFrame pic;
  41. int range;
  42. uint8_t *comp_buf, *work_buf;
  43. uint8_t pal[768];
  44. uint32_t pal2[256]; //for quick comparisons
  45. uint8_t *prev;
  46. int pstride;
  47. int comp_size;
  48. int keyint, curfrm;
  49. z_stream zstream;
  50. } ZmbvEncContext;
  51. static int score_tab[256];
  52. /** Block comparing function
  53. * XXX should be optimized and moved to DSPContext
  54. * TODO handle out of edge ME
  55. */
  56. static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2,
  57. int bw, int bh, 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 += 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(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(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. AVFrame * const p = &c->pic;
  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. *p = *pict;
  121. p->pict_type= keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  122. p->key_frame= keyframe;
  123. chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
  124. palptr = (uint32_t*)p->data[1];
  125. src = p->data[0];
  126. prev = c->prev;
  127. if(chpal){
  128. uint8_t tpal[3];
  129. for(i = 0; i < 256; i++){
  130. AV_WB24(tpal, palptr[i]);
  131. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  132. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  133. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  134. c->pal[i * 3 + 0] = tpal[0];
  135. c->pal[i * 3 + 1] = tpal[1];
  136. c->pal[i * 3 + 2] = tpal[2];
  137. }
  138. memcpy(c->pal2, p->data[1], 1024);
  139. }
  140. if(keyframe){
  141. for(i = 0; i < 256; i++){
  142. AV_WB24(c->pal+(i*3), palptr[i]);
  143. }
  144. memcpy(c->work_buf, c->pal, 768);
  145. memcpy(c->pal2, p->data[1], 1024);
  146. work_size = 768;
  147. for(i = 0; i < avctx->height; i++){
  148. memcpy(c->work_buf + work_size, src, avctx->width);
  149. src += p->linesize[0];
  150. work_size += avctx->width;
  151. }
  152. }else{
  153. int x, y, bh2, bw2, xored;
  154. uint8_t *tsrc, *tprev;
  155. uint8_t *mv;
  156. int mx, my;
  157. bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  158. bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  159. mv = c->work_buf + work_size;
  160. memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
  161. work_size += (bw * bh * 2 + 3) & ~3;
  162. /* for now just XOR'ing */
  163. for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
  164. bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
  165. for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
  166. bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
  167. tsrc = src + x;
  168. tprev = prev + x;
  169. zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
  170. mv[0] = (mx << 1) | !!xored;
  171. mv[1] = my << 1;
  172. tprev += mx + my * c->pstride;
  173. if(xored){
  174. for(j = 0; j < bh2; j++){
  175. for(i = 0; i < bw2; i++)
  176. c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
  177. tsrc += p->linesize[0];
  178. tprev += c->pstride;
  179. }
  180. }
  181. }
  182. src += p->linesize[0] * ZMBV_BLOCK;
  183. prev += c->pstride * ZMBV_BLOCK;
  184. }
  185. }
  186. /* save the previous frame */
  187. src = p->data[0];
  188. prev = c->prev;
  189. for(i = 0; i < avctx->height; i++){
  190. memcpy(prev, src, avctx->width);
  191. prev += c->pstride;
  192. src += p->linesize[0];
  193. }
  194. if (keyframe)
  195. deflateReset(&c->zstream);
  196. c->zstream.next_in = c->work_buf;
  197. c->zstream.avail_in = work_size;
  198. c->zstream.total_in = 0;
  199. c->zstream.next_out = c->comp_buf;
  200. c->zstream.avail_out = c->comp_size;
  201. c->zstream.total_out = 0;
  202. if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
  203. av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
  204. return -1;
  205. }
  206. pkt_size = c->zstream.total_out + 1 + 6*keyframe;
  207. if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size)) < 0)
  208. return ret;
  209. buf = pkt->data;
  210. fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
  211. *buf++ = fl;
  212. if (keyframe) {
  213. *buf++ = 0; // hi ver
  214. *buf++ = 1; // lo ver
  215. *buf++ = 1; // comp
  216. *buf++ = 4; // format - 8bpp
  217. *buf++ = ZMBV_BLOCK; // block width
  218. *buf++ = ZMBV_BLOCK; // block height
  219. }
  220. memcpy(buf, c->comp_buf, c->zstream.total_out);
  221. pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
  222. *got_packet = 1;
  223. return 0;
  224. }
  225. /**
  226. * Init zmbv encoder
  227. */
  228. static av_cold int encode_init(AVCodecContext *avctx)
  229. {
  230. ZmbvEncContext * const c = avctx->priv_data;
  231. int zret; // Zlib return code
  232. int i;
  233. int lvl = 9;
  234. for(i=1; i<256; i++)
  235. score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
  236. c->avctx = avctx;
  237. c->curfrm = 0;
  238. c->keyint = avctx->keyint_min;
  239. c->range = 8;
  240. if(avctx->me_range > 0)
  241. c->range = FFMIN(avctx->me_range, 127);
  242. if(avctx->compression_level >= 0)
  243. lvl = avctx->compression_level;
  244. if(lvl < 0 || lvl > 9){
  245. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  246. return AVERROR(EINVAL);
  247. }
  248. // Needed if zlib unused or init aborted before deflateInit
  249. memset(&c->zstream, 0, sizeof(z_stream));
  250. c->comp_size = avctx->width * avctx->height + 1024 +
  251. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  252. if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
  253. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  254. return AVERROR(ENOMEM);
  255. }
  256. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  257. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  258. ((c->comp_size + 63) >> 6) + 11;
  259. /* Allocate compression buffer */
  260. if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
  261. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  262. return AVERROR(ENOMEM);
  263. }
  264. c->pstride = FFALIGN(avctx->width, 16);
  265. if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
  266. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  267. return AVERROR(ENOMEM);
  268. }
  269. c->zstream.zalloc = Z_NULL;
  270. c->zstream.zfree = Z_NULL;
  271. c->zstream.opaque = Z_NULL;
  272. zret = deflateInit(&c->zstream, lvl);
  273. if (zret != Z_OK) {
  274. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  275. return -1;
  276. }
  277. avctx->coded_frame = &c->pic;
  278. return 0;
  279. }
  280. /**
  281. * Uninit zmbv encoder
  282. */
  283. static av_cold int encode_end(AVCodecContext *avctx)
  284. {
  285. ZmbvEncContext * const c = avctx->priv_data;
  286. av_freep(&c->comp_buf);
  287. av_freep(&c->work_buf);
  288. deflateEnd(&c->zstream);
  289. av_freep(&c->prev);
  290. return 0;
  291. }
  292. AVCodec ff_zmbv_encoder = {
  293. .name = "zmbv",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .id = AV_CODEC_ID_ZMBV,
  296. .priv_data_size = sizeof(ZmbvEncContext),
  297. .init = encode_init,
  298. .encode2 = encode_frame,
  299. .close = encode_end,
  300. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE },
  301. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  302. };