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.

449 lines
14KB

  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. /* Frame header flags */
  33. #define ZMBV_KEYFRAME 1
  34. #define ZMBV_DELTAPAL 2
  35. /* Motion block width/height (maximum allowed value is 255)
  36. * Note: histogram datatype in block_cmp() must be big enough to hold values
  37. * up to (4 * ZMBV_BLOCK * ZMBV_BLOCK)
  38. */
  39. #define ZMBV_BLOCK 16
  40. /* Keyframe header format values */
  41. enum ZmbvFormat {
  42. ZMBV_FMT_NONE = 0,
  43. ZMBV_FMT_1BPP = 1,
  44. ZMBV_FMT_2BPP = 2,
  45. ZMBV_FMT_4BPP = 3,
  46. ZMBV_FMT_8BPP = 4,
  47. ZMBV_FMT_15BPP = 5,
  48. ZMBV_FMT_16BPP = 6,
  49. ZMBV_FMT_24BPP = 7,
  50. ZMBV_FMT_32BPP = 8
  51. };
  52. /**
  53. * Encoder context
  54. */
  55. typedef struct ZmbvEncContext {
  56. AVCodecContext *avctx;
  57. int lrange, urange;
  58. uint8_t *comp_buf, *work_buf;
  59. uint8_t pal[768];
  60. uint32_t pal2[256]; //for quick comparisons
  61. uint8_t *prev, *prev_buf;
  62. int pstride;
  63. int comp_size;
  64. int keyint, curfrm;
  65. int bypp;
  66. enum ZmbvFormat fmt;
  67. z_stream zstream;
  68. int score_tab[ZMBV_BLOCK * ZMBV_BLOCK * 4 + 1];
  69. } ZmbvEncContext;
  70. /** Block comparing function
  71. * XXX should be optimized and moved to DSPContext
  72. */
  73. static inline int block_cmp(ZmbvEncContext *c, uint8_t *src, int stride,
  74. uint8_t *src2, int stride2, int bw, int bh,
  75. int *xored)
  76. {
  77. int sum = 0;
  78. int i, j;
  79. uint16_t histogram[256] = {0};
  80. int bw_bytes = bw * c->bypp;
  81. /* Build frequency histogram of byte values for src[] ^ src2[] */
  82. for(j = 0; j < bh; j++){
  83. for(i = 0; i < bw_bytes; i++){
  84. int t = src[i] ^ src2[i];
  85. histogram[t]++;
  86. }
  87. src += stride;
  88. src2 += stride2;
  89. }
  90. /* If not all the xored values were 0, then the blocks are different */
  91. *xored = (histogram[0] < bw_bytes * bh);
  92. /* Exit early if blocks are equal */
  93. if (!*xored) return 0;
  94. /* Sum the entropy of all values */
  95. for(i = 0; i < 256; i++)
  96. sum += c->score_tab[histogram[i]];
  97. return sum;
  98. }
  99. /** Motion estimation function
  100. * TODO make better ME decisions
  101. */
  102. static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
  103. int pstride, int x, int y, int *mx, int *my, int *xored)
  104. {
  105. int dx, dy, txored, tv, bv, bw, bh;
  106. int mx0, my0;
  107. mx0 = *mx;
  108. my0 = *my;
  109. bw = FFMIN(ZMBV_BLOCK, c->avctx->width - x);
  110. bh = FFMIN(ZMBV_BLOCK, c->avctx->height - y);
  111. /* Try (0,0) */
  112. bv = block_cmp(c, src, sstride, prev, pstride, bw, bh, xored);
  113. *mx = *my = 0;
  114. if(!bv) return 0;
  115. /* Try previous block's MV (if not 0,0) */
  116. if (mx0 || my0){
  117. tv = block_cmp(c, src, sstride, prev + mx0 * c->bypp + my0 * pstride, pstride, bw, bh, &txored);
  118. if(tv < bv){
  119. bv = tv;
  120. *mx = mx0;
  121. *my = my0;
  122. *xored = txored;
  123. if(!bv) return 0;
  124. }
  125. }
  126. /* Try other MVs from top-to-bottom, left-to-right */
  127. for(dy = -c->lrange; dy <= c->urange; dy++){
  128. for(dx = -c->lrange; dx <= c->urange; dx++){
  129. if(!dx && !dy) continue; // we already tested this block
  130. if(dx == mx0 && dy == my0) continue; // this one too
  131. tv = block_cmp(c, src, sstride, prev + dx * c->bypp + dy * pstride, pstride, bw, bh, &txored);
  132. if(tv < bv){
  133. bv = tv;
  134. *mx = dx;
  135. *my = dy;
  136. *xored = txored;
  137. if(!bv) return 0;
  138. }
  139. }
  140. }
  141. return bv;
  142. }
  143. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  144. const AVFrame *pict, int *got_packet)
  145. {
  146. ZmbvEncContext * const c = avctx->priv_data;
  147. const AVFrame * const p = pict;
  148. uint8_t *src, *prev, *buf;
  149. uint32_t *palptr;
  150. int keyframe, chpal;
  151. int fl;
  152. int work_size = 0, pkt_size;
  153. int bw, bh;
  154. int i, j, ret;
  155. keyframe = !c->curfrm;
  156. c->curfrm++;
  157. if(c->curfrm == c->keyint)
  158. c->curfrm = 0;
  159. #if FF_API_CODED_FRAME
  160. FF_DISABLE_DEPRECATION_WARNINGS
  161. avctx->coded_frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  162. avctx->coded_frame->key_frame = keyframe;
  163. FF_ENABLE_DEPRECATION_WARNINGS
  164. #endif
  165. palptr = (avctx->pix_fmt == AV_PIX_FMT_PAL8) ? (uint32_t *)p->data[1] : NULL;
  166. chpal = !keyframe && palptr && memcmp(palptr, c->pal2, 1024);
  167. src = p->data[0];
  168. prev = c->prev;
  169. if(chpal){
  170. uint8_t tpal[3];
  171. for(i = 0; i < 256; i++){
  172. AV_WB24(tpal, palptr[i]);
  173. c->work_buf[work_size++] = tpal[0] ^ c->pal[i * 3 + 0];
  174. c->work_buf[work_size++] = tpal[1] ^ c->pal[i * 3 + 1];
  175. c->work_buf[work_size++] = tpal[2] ^ c->pal[i * 3 + 2];
  176. c->pal[i * 3 + 0] = tpal[0];
  177. c->pal[i * 3 + 1] = tpal[1];
  178. c->pal[i * 3 + 2] = tpal[2];
  179. }
  180. memcpy(c->pal2, palptr, 1024);
  181. }
  182. if(keyframe){
  183. if (palptr){
  184. for(i = 0; i < 256; i++){
  185. AV_WB24(c->pal+(i*3), palptr[i]);
  186. }
  187. memcpy(c->work_buf, c->pal, 768);
  188. memcpy(c->pal2, palptr, 1024);
  189. work_size = 768;
  190. }
  191. for(i = 0; i < avctx->height; i++){
  192. memcpy(c->work_buf + work_size, src, avctx->width * c->bypp);
  193. src += p->linesize[0];
  194. work_size += avctx->width * c->bypp;
  195. }
  196. }else{
  197. int x, y, bh2, bw2, xored;
  198. uint8_t *tsrc, *tprev;
  199. uint8_t *mv;
  200. int mx = 0, my = 0;
  201. bw = (avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  202. bh = (avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK;
  203. mv = c->work_buf + work_size;
  204. memset(c->work_buf + work_size, 0, (bw * bh * 2 + 3) & ~3);
  205. work_size += (bw * bh * 2 + 3) & ~3;
  206. /* for now just XOR'ing */
  207. for(y = 0; y < avctx->height; y += ZMBV_BLOCK) {
  208. bh2 = FFMIN(avctx->height - y, ZMBV_BLOCK);
  209. for(x = 0; x < avctx->width; x += ZMBV_BLOCK, mv += 2) {
  210. bw2 = FFMIN(avctx->width - x, ZMBV_BLOCK);
  211. tsrc = src + x * c->bypp;
  212. tprev = prev + x * c->bypp;
  213. zmbv_me(c, tsrc, p->linesize[0], tprev, c->pstride, x, y, &mx, &my, &xored);
  214. mv[0] = (mx * 2) | !!xored;
  215. mv[1] = my * 2;
  216. tprev += mx * c->bypp + my * c->pstride;
  217. if(xored){
  218. for(j = 0; j < bh2; j++){
  219. for(i = 0; i < bw2 * c->bypp; i++)
  220. c->work_buf[work_size++] = tsrc[i] ^ tprev[i];
  221. tsrc += p->linesize[0];
  222. tprev += c->pstride;
  223. }
  224. }
  225. }
  226. src += p->linesize[0] * ZMBV_BLOCK;
  227. prev += c->pstride * ZMBV_BLOCK;
  228. }
  229. }
  230. /* save the previous frame */
  231. src = p->data[0];
  232. prev = c->prev;
  233. for(i = 0; i < avctx->height; i++){
  234. memcpy(prev, src, avctx->width * c->bypp);
  235. prev += c->pstride;
  236. src += p->linesize[0];
  237. }
  238. if (keyframe)
  239. deflateReset(&c->zstream);
  240. c->zstream.next_in = c->work_buf;
  241. c->zstream.avail_in = work_size;
  242. c->zstream.total_in = 0;
  243. c->zstream.next_out = c->comp_buf;
  244. c->zstream.avail_out = c->comp_size;
  245. c->zstream.total_out = 0;
  246. if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
  247. av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
  248. return -1;
  249. }
  250. pkt_size = c->zstream.total_out + 1 + 6*keyframe;
  251. if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
  252. return ret;
  253. buf = pkt->data;
  254. fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
  255. *buf++ = fl;
  256. if (keyframe) {
  257. *buf++ = 0; // hi ver
  258. *buf++ = 1; // lo ver
  259. *buf++ = 1; // comp
  260. *buf++ = c->fmt; // format
  261. *buf++ = ZMBV_BLOCK; // block width
  262. *buf++ = ZMBV_BLOCK; // block height
  263. }
  264. memcpy(buf, c->comp_buf, c->zstream.total_out);
  265. pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
  266. *got_packet = 1;
  267. return 0;
  268. }
  269. static av_cold int encode_end(AVCodecContext *avctx)
  270. {
  271. ZmbvEncContext * const c = avctx->priv_data;
  272. av_freep(&c->comp_buf);
  273. av_freep(&c->work_buf);
  274. deflateEnd(&c->zstream);
  275. av_freep(&c->prev_buf);
  276. return 0;
  277. }
  278. /**
  279. * Init zmbv encoder
  280. */
  281. static av_cold int encode_init(AVCodecContext *avctx)
  282. {
  283. ZmbvEncContext * const c = avctx->priv_data;
  284. int zret; // Zlib return code
  285. int i;
  286. int lvl = 9;
  287. int prev_size, prev_offset;
  288. switch (avctx->pix_fmt) {
  289. case AV_PIX_FMT_PAL8:
  290. c->fmt = ZMBV_FMT_8BPP;
  291. c->bypp = 1;
  292. break;
  293. case AV_PIX_FMT_RGB555LE:
  294. c->fmt = ZMBV_FMT_15BPP;
  295. c->bypp = 2;
  296. break;
  297. case AV_PIX_FMT_RGB565LE:
  298. c->fmt = ZMBV_FMT_16BPP;
  299. c->bypp = 2;
  300. break;
  301. #ifdef ZMBV_ENABLE_24BPP
  302. case AV_PIX_FMT_BGR24:
  303. c->fmt = ZMBV_FMT_24BPP;
  304. c->bypp = 3;
  305. break;
  306. #endif //ZMBV_ENABLE_24BPP
  307. case AV_PIX_FMT_BGR0:
  308. c->fmt = ZMBV_FMT_32BPP;
  309. c->bypp = 4;
  310. break;
  311. default:
  312. av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
  313. return AVERROR(EINVAL);
  314. }
  315. /* Entropy-based score tables for comparing blocks.
  316. * Suitable for blocks up to (ZMBV_BLOCK * ZMBV_BLOCK) bytes.
  317. * Scores are nonnegative, lower is better.
  318. */
  319. for(i = 1; i <= ZMBV_BLOCK * ZMBV_BLOCK * c->bypp; i++)
  320. c->score_tab[i] = -i * log2(i / (double)(ZMBV_BLOCK * ZMBV_BLOCK * c->bypp)) * 256;
  321. c->avctx = avctx;
  322. c->curfrm = 0;
  323. c->keyint = avctx->keyint_min;
  324. /* Motion estimation range: maximum distance is -64..63 */
  325. c->lrange = c->urange = 8;
  326. if(avctx->me_range > 0){
  327. c->lrange = FFMIN(avctx->me_range, 64);
  328. c->urange = FFMIN(avctx->me_range, 63);
  329. }
  330. if(avctx->compression_level >= 0)
  331. lvl = avctx->compression_level;
  332. if(lvl < 0 || lvl > 9){
  333. av_log(avctx, AV_LOG_ERROR, "Compression level should be 0-9, not %i\n", lvl);
  334. return AVERROR(EINVAL);
  335. }
  336. // Needed if zlib unused or init aborted before deflateInit
  337. memset(&c->zstream, 0, sizeof(z_stream));
  338. c->comp_size = avctx->width * c->bypp * avctx->height + 1024 +
  339. ((avctx->width + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * ((avctx->height + ZMBV_BLOCK - 1) / ZMBV_BLOCK) * 2 + 4;
  340. if (!(c->work_buf = av_malloc(c->comp_size))) {
  341. av_log(avctx, AV_LOG_ERROR, "Can't allocate work buffer.\n");
  342. return AVERROR(ENOMEM);
  343. }
  344. /* Conservative upper bound taken from zlib v1.2.1 source via lcl.c */
  345. c->comp_size = c->comp_size + ((c->comp_size + 7) >> 3) +
  346. ((c->comp_size + 63) >> 6) + 11;
  347. /* Allocate compression buffer */
  348. if (!(c->comp_buf = av_malloc(c->comp_size))) {
  349. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  350. return AVERROR(ENOMEM);
  351. }
  352. /* Allocate prev buffer - pad around the image to allow out-of-edge ME:
  353. * - The image should be padded with `lrange` rows before and `urange` rows
  354. * after.
  355. * - The stride should be padded with `lrange` pixels, then rounded up to a
  356. * multiple of 16 bytes.
  357. * - The first row should also be padded with `lrange` pixels before, then
  358. * aligned up to a multiple of 16 bytes.
  359. */
  360. c->pstride = FFALIGN((avctx->width + c->lrange) * c->bypp, 16);
  361. prev_size = FFALIGN(c->lrange * c->bypp, 16) + c->pstride * (c->lrange + avctx->height + c->urange);
  362. prev_offset = FFALIGN(c->lrange, 16) + c->pstride * c->lrange;
  363. if (!(c->prev_buf = av_mallocz(prev_size))) {
  364. av_log(avctx, AV_LOG_ERROR, "Can't allocate picture.\n");
  365. return AVERROR(ENOMEM);
  366. }
  367. c->prev = c->prev_buf + prev_offset;
  368. c->zstream.zalloc = Z_NULL;
  369. c->zstream.zfree = Z_NULL;
  370. c->zstream.opaque = Z_NULL;
  371. zret = deflateInit(&c->zstream, lvl);
  372. if (zret != Z_OK) {
  373. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  374. return -1;
  375. }
  376. return 0;
  377. }
  378. AVCodec ff_zmbv_encoder = {
  379. .name = "zmbv",
  380. .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
  381. .type = AVMEDIA_TYPE_VIDEO,
  382. .id = AV_CODEC_ID_ZMBV,
  383. .priv_data_size = sizeof(ZmbvEncContext),
  384. .init = encode_init,
  385. .encode2 = encode_frame,
  386. .close = encode_end,
  387. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_PAL8,
  388. AV_PIX_FMT_RGB555LE,
  389. AV_PIX_FMT_RGB565LE,
  390. #ifdef ZMBV_ENABLE_24BPP
  391. AV_PIX_FMT_BGR24,
  392. #endif //ZMBV_ENABLE_24BPP
  393. AV_PIX_FMT_BGR0,
  394. AV_PIX_FMT_NONE },
  395. };