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.

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