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.

337 lines
9.7KB

  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 libavcodec/zmbvenc.c
  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 <zlib.h>
  30. #define ZMBV_KEYFRAME 1
  31. #define ZMBV_DELTAPAL 2
  32. #define ZMBV_BLOCK 16
  33. /**
  34. * Encoder context
  35. */
  36. typedef struct ZmbvEncContext {
  37. AVCodecContext *avctx;
  38. AVFrame pic;
  39. int range;
  40. uint8_t *comp_buf, *work_buf;
  41. uint8_t pal[768];
  42. uint32_t pal2[256]; //for quick comparisons
  43. uint8_t *prev;
  44. int pstride;
  45. int comp_size;
  46. int keyint, curfrm;
  47. z_stream zstream;
  48. } ZmbvEncContext;
  49. static int score_tab[256];
  50. /** Block comparing function
  51. * XXX should be optimized and moved to DSPContext
  52. * TODO handle out of edge ME
  53. */
  54. static inline int block_cmp(uint8_t *src, int stride, uint8_t *src2, int stride2,
  55. int bw, int bh, int *xored)
  56. {
  57. int sum = 0;
  58. int i, j;
  59. uint8_t histogram[256] = {0};
  60. *xored = 0;
  61. for(j = 0; j < bh; j++){
  62. for(i = 0; i < bw; i++){
  63. int t = src[i] ^ src2[i];
  64. histogram[t]++;
  65. *xored |= t;
  66. }
  67. src += stride;
  68. src2 += stride2;
  69. }
  70. for(i = 1; i < 256; i++)
  71. sum += score_tab[histogram[i]];
  72. return sum;
  73. }
  74. /** Motion estimation function
  75. * TODO make better ME decisions
  76. */
  77. static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
  78. int pstride, int x, int y, int *mx, int *my, int *xored)
  79. {
  80. int dx, dy, tx, ty, tv, bv, bw, bh;
  81. *mx = *my = 0;
  82. bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
  83. bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
  84. bv = block_cmp(src, sstride, prev, pstride, bw, bh, xored);
  85. if(!bv) return 0;
  86. for(ty = FFMAX(y - c->range, 0); ty < FFMIN(y + c->range, c->avctx->height - bh); ty++){
  87. for(tx = FFMAX(x - c->range, 0); tx < FFMIN(x + c->range, c->avctx->width - bw); tx++){
  88. if(tx == x && ty == y) continue; // we already tested this block
  89. dx = tx - x;
  90. dy = ty - y;
  91. tv = block_cmp(src, sstride, prev + dx + dy*pstride, pstride, bw, bh, xored);
  92. if(tv < bv){
  93. bv = tv;
  94. *mx = dx;
  95. *my = dy;
  96. if(!bv) return 0;
  97. }
  98. }
  99. }
  100. return bv;
  101. }
  102. static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
  103. {
  104. ZmbvEncContext * const c = avctx->priv_data;
  105. AVFrame *pict = data;
  106. AVFrame * const p = &c->pic;
  107. uint8_t *src, *prev;
  108. uint32_t *palptr;
  109. int len = 0;
  110. int keyframe, chpal;
  111. int fl;
  112. int work_size = 0;
  113. int bw, bh;
  114. int i, j;
  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 ? FF_I_TYPE : FF_P_TYPE;
  121. p->key_frame= keyframe;
  122. chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
  123. fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
  124. *buf++ = fl; len++;
  125. if(keyframe){
  126. deflateReset(&c->zstream);
  127. *buf++ = 0; len++; // hi ver
  128. *buf++ = 1; len++; // lo ver
  129. *buf++ = 1; len++; // comp
  130. *buf++ = 4; len++; // format - 8bpp
  131. *buf++ = ZMBV_BLOCK; len++; // block width
  132. *buf++ = ZMBV_BLOCK; len++; // block height
  133. }
  134. palptr = (uint32_t*)p->data[1];
  135. src = p->data[0];
  136. prev = c->prev;
  137. if(chpal){
  138. uint8_t tpal[3];
  139. for(i = 0; i < 256; i++){
  140. AV_WB24(tpal, palptr[i]);
  141. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  142. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  143. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  144. c->pal[i * 3 + 0] = tpal[0];
  145. c->pal[i * 3 + 1] = tpal[1];
  146. c->pal[i * 3 + 2] = tpal[2];
  147. }
  148. memcpy(c->pal2, p->data[1], 1024);
  149. }
  150. if(keyframe){
  151. for(i = 0; i < 256; i++){
  152. AV_WB24(c->pal+(i*3), palptr[i]);
  153. }
  154. memcpy(c->work_buf, c->pal, 768);
  155. memcpy(c->pal2, p->data[1], 1024);
  156. work_size = 768;
  157. for(i = 0; i < avctx->height; i++){
  158. memcpy(c->work_buf + work_size, src, avctx->width);
  159. src += p->linesize[0];
  160. work_size += avctx->width;
  161. }
  162. }else{
  163. int x, y, bh2, bw2, xored;
  164. uint8_t *tsrc, *tprev;
  165. uint8_t *mv;
  166. int mx, my, bv;
  167. bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  168. bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  169. mv = c->work_buf + work_size;
  170. memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
  171. work_size += (bw * bh * 2 + 3) & ~3;
  172. /* for now just XOR'ing */
  173. for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
  174. bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
  175. for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
  176. bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
  177. tsrc = src + x;
  178. tprev = prev + x;
  179. bv = zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
  180. mv[0] = (mx << 1) | !!xored;
  181. mv[1] = my << 1;
  182. tprev += mx + my * c->pstride;
  183. if(xored){
  184. for(j = 0; j < bh2; j++){
  185. for(i = 0; i < bw2; i++)
  186. c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
  187. tsrc += p->linesize[0];
  188. tprev += c->pstride;
  189. }
  190. }
  191. }
  192. src += p->linesize[0] * ZMBV_BLOCK;
  193. prev += c->pstride * ZMBV_BLOCK;
  194. }
  195. }
  196. /* save the previous frame */
  197. src = p->data[0];
  198. prev = c->prev;
  199. for(i = 0; i < avctx->height; i++){
  200. memcpy(prev, src, avctx->width);
  201. prev += c->pstride;
  202. src += p->linesize[0];
  203. }
  204. c->zstream.next_in = c->work_buf;
  205. c->zstream.avail_in = work_size;
  206. c->zstream.total_in = 0;
  207. c->zstream.next_out = c->comp_buf;
  208. c->zstream.avail_out = c->comp_size;
  209. c->zstream.total_out = 0;
  210. if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
  211. av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
  212. return -1;
  213. }
  214. memcpy(buf, c->comp_buf, c->zstream.total_out);
  215. return len + c->zstream.total_out;
  216. }
  217. /**
  218. * Init zmbv encoder
  219. */
  220. static av_cold int encode_init(AVCodecContext *avctx)
  221. {
  222. ZmbvEncContext * const c = avctx->priv_data;
  223. int zret; // Zlib return code
  224. int i;
  225. int lvl = 9;
  226. for(i=1; i<256; i++)
  227. score_tab[i]= -i * log(i/(double)(ZMBV_BLOCK*ZMBV_BLOCK)) * (256/M_LN2);
  228. c->avctx = avctx;
  229. c->curfrm = 0;
  230. c->keyint = avctx->keyint_min;
  231. c->range = 8;
  232. if(avctx->me_range > 0)
  233. c->range = FFMIN(avctx->me_range, 127);
  234. if(avctx->compression_level >= 0)
  235. lvl = avctx->compression_level;
  236. if(lvl < 0 || lvl > 9){
  237. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  238. return -1;
  239. }
  240. // Needed if zlib unused or init aborted before deflateInit
  241. memset(&(c->zstream), 0, sizeof(z_stream));
  242. c->comp_size = avctx->width * avctx->height + 1024 +
  243. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  244. if ((c->work_buf = av_malloc(c->comp_size)) == NULL) {
  245. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  246. return -1;
  247. }
  248. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  249. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  250. ((c->comp_size + 63) >> 6) + 11;
  251. /* Allocate compression buffer */
  252. if ((c->comp_buf = av_malloc(c->comp_size)) == NULL) {
  253. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  254. return -1;
  255. }
  256. c->pstride = FFALIGN(avctx->width, 16);
  257. if ((c->prev = av_malloc(c->pstride * avctx->height)) == NULL) {
  258. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  259. return -1;
  260. }
  261. c->zstream.zalloc = Z_NULL;
  262. c->zstream.zfree = Z_NULL;
  263. c->zstream.opaque = Z_NULL;
  264. zret = deflateInit(&(c->zstream), lvl);
  265. if (zret != Z_OK) {
  266. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  267. return -1;
  268. }
  269. avctx->coded_frame = (AVFrame*)&c->pic;
  270. return 0;
  271. }
  272. /**
  273. * Uninit zmbv encoder
  274. */
  275. static av_cold int encode_end(AVCodecContext *avctx)
  276. {
  277. ZmbvEncContext * const c = avctx->priv_data;
  278. av_freep(&c->comp_buf);
  279. av_freep(&c->work_buf);
  280. deflateEnd(&(c->zstream));
  281. av_freep(&c->prev);
  282. return 0;
  283. }
  284. AVCodec zmbv_encoder = {
  285. "zmbv",
  286. CODEC_TYPE_VIDEO,
  287. CODEC_ID_ZMBV,
  288. sizeof(ZmbvEncContext),
  289. encode_init,
  290. encode_frame,
  291. encode_end,
  292. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
  293. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  294. };