flashsv2enc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * Flash Screen Video Version 2 encoder
  3. * Copyright (C) 2009 Joshua Warner
  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. * Flash Screen Video Version 2 encoder
  24. * @author Joshua Warner
  25. */
  26. /* Differences from version 1 stream:
  27. * NOTE: Currently, the only player that supports version 2 streams is Adobe Flash Player itself.
  28. * * Supports sending only a range of scanlines in a block,
  29. * indicating a difference from the corresponding block in the last keyframe.
  30. * * Supports initializing the zlib dictionary with data from the corresponding
  31. * block in the last keyframe, to improve compression.
  32. * * Supports a hybrid 15-bit rgb / 7-bit palette color space.
  33. */
  34. /* TODO:
  35. * Don't keep Block structures for both current frame and keyframe.
  36. * Make better heuristics for deciding stream parameters (optimum_* functions). Currently these return constants.
  37. * Figure out how to encode palette information in the stream, choose an optimum palette at each keyframe.
  38. * Figure out how the zlibPrimeCompressCurrent flag works, implement support.
  39. * Find other sample files (that weren't generated here), develop a decoder.
  40. */
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <zlib.h>
  44. #include "libavutil/imgutils.h"
  45. #include "avcodec.h"
  46. #include "put_bits.h"
  47. #include "bytestream.h"
  48. #define HAS_IFRAME_IMAGE 0x02
  49. #define HAS_PALLET_INFO 0x01
  50. #define COLORSPACE_BGR 0x00
  51. #define COLORSPACE_15_7 0x10
  52. #define HAS_DIFF_BLOCKS 0x04
  53. #define ZLIB_PRIME_COMPRESS_CURRENT 0x02
  54. #define ZLIB_PRIME_COMPRESS_PREVIOUS 0x01
  55. // Disables experimental "smart" parameter-choosing code, as well as the statistics that it depends on.
  56. // At the moment, the "smart" code is a great example of how the parameters *shouldn't* be chosen.
  57. #define FLASHSV2_DUMB
  58. typedef struct Block {
  59. uint8_t *enc;
  60. uint8_t *sl_begin, *sl_end;
  61. int enc_size;
  62. uint8_t *data;
  63. unsigned long data_size;
  64. uint8_t start, len;
  65. uint8_t dirty;
  66. uint8_t col, row, width, height;
  67. uint8_t flags;
  68. } Block;
  69. typedef struct Palette {
  70. unsigned colors[128];
  71. uint8_t index[1 << 15];
  72. } Palette;
  73. typedef struct FlashSV2Context {
  74. AVCodecContext *avctx;
  75. uint8_t *current_frame;
  76. uint8_t *key_frame;
  77. AVFrame frame;
  78. uint8_t *encbuffer;
  79. uint8_t *keybuffer;
  80. uint8_t *databuffer;
  81. Block *frame_blocks;
  82. Block *key_blocks;
  83. int frame_size;
  84. int blocks_size;
  85. int use15_7, dist, comp;
  86. int rows, cols;
  87. int last_key_frame;
  88. int image_width, image_height;
  89. int block_width, block_height;
  90. uint8_t flags;
  91. uint8_t use_custom_palette;
  92. uint8_t palette_type; ///< 0=>default, 1=>custom - changed when palette regenerated.
  93. Palette palette;
  94. #ifndef FLASHSV2_DUMB
  95. double tot_blocks; ///< blocks encoded since last keyframe
  96. double diff_blocks; ///< blocks that were different since last keyframe
  97. double tot_lines; ///< total scanlines in image since last keyframe
  98. double diff_lines; ///< scanlines that were different since last keyframe
  99. double raw_size; ///< size of raw frames since last keyframe
  100. double comp_size; ///< size of compressed data since last keyframe
  101. double uncomp_size; ///< size of uncompressed data since last keyframe
  102. double total_bits; ///< total bits written to stream so far
  103. #endif
  104. } FlashSV2Context;
  105. static av_cold void cleanup(FlashSV2Context * s)
  106. {
  107. av_freep(&s->encbuffer);
  108. av_freep(&s->keybuffer);
  109. av_freep(&s->databuffer);
  110. av_freep(&s->current_frame);
  111. av_freep(&s->key_frame);
  112. av_freep(&s->frame_blocks);
  113. av_freep(&s->key_blocks);
  114. }
  115. static void init_blocks(FlashSV2Context * s, Block * blocks,
  116. uint8_t * encbuf, uint8_t * databuf)
  117. {
  118. int row, col;
  119. Block *b;
  120. for (col = 0; col < s->cols; col++) {
  121. for (row = 0; row < s->rows; row++) {
  122. b = blocks + (col + row * s->cols);
  123. b->width = (col < s->cols - 1) ?
  124. s->block_width :
  125. s->image_width - col * s->block_width;
  126. b->height = (row < s->rows - 1) ?
  127. s->block_height :
  128. s->image_height - row * s->block_height;
  129. b->row = row;
  130. b->col = col;
  131. b->enc = encbuf;
  132. b->data = databuf;
  133. encbuf += b->width * b->height * 3;
  134. databuf += !databuf ? 0 : b->width * b->height * 6;
  135. }
  136. }
  137. }
  138. static void reset_stats(FlashSV2Context * s)
  139. {
  140. #ifndef FLASHSV2_DUMB
  141. s->diff_blocks = 0.1;
  142. s->tot_blocks = 1;
  143. s->diff_lines = 0.1;
  144. s->tot_lines = 1;
  145. s->raw_size = s->comp_size = s->uncomp_size = 10;
  146. #endif
  147. }
  148. static av_cold int flashsv2_encode_init(AVCodecContext * avctx)
  149. {
  150. FlashSV2Context *s = avctx->priv_data;
  151. s->avctx = avctx;
  152. s->comp = avctx->compression_level;
  153. if (s->comp == -1)
  154. s->comp = 9;
  155. if (s->comp < 0 || s->comp > 9) {
  156. av_log(avctx, AV_LOG_ERROR,
  157. "Compression level should be 0-9, not %d\n", s->comp);
  158. return -1;
  159. }
  160. if ((avctx->width > 4095) || (avctx->height > 4095)) {
  161. av_log(avctx, AV_LOG_ERROR,
  162. "Input dimensions too large, input must be max 4096x4096 !\n");
  163. return -1;
  164. }
  165. if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)
  166. return -1;
  167. s->last_key_frame = 0;
  168. s->image_width = avctx->width;
  169. s->image_height = avctx->height;
  170. s->block_width = (s->image_width / 12) & ~15;
  171. s->block_height = (s->image_height / 12) & ~15;
  172. s->rows = (s->image_height + s->block_height - 1) / s->block_height;
  173. s->cols = (s->image_width + s->block_width - 1) / s->block_width;
  174. s->frame_size = s->image_width * s->image_height * 3;
  175. s->blocks_size = s->rows * s->cols * sizeof(Block);
  176. s->encbuffer = av_mallocz(s->frame_size);
  177. s->keybuffer = av_mallocz(s->frame_size);
  178. s->databuffer = av_mallocz(s->frame_size * 6);
  179. s->current_frame = av_mallocz(s->frame_size);
  180. s->key_frame = av_mallocz(s->frame_size);
  181. s->frame_blocks = av_mallocz(s->blocks_size);
  182. s->key_blocks = av_mallocz(s->blocks_size);
  183. init_blocks(s, s->frame_blocks, s->encbuffer, s->databuffer);
  184. init_blocks(s, s->key_blocks, s->keybuffer, 0);
  185. reset_stats(s);
  186. #ifndef FLASHSV2_DUMB
  187. s->total_bits = 1;
  188. #endif
  189. s->use_custom_palette = 0;
  190. s->palette_type = -1; // so that the palette will be generated in reconfigure_at_keyframe
  191. if (!s->encbuffer || !s->keybuffer || !s->databuffer
  192. || !s->current_frame || !s->key_frame || !s->key_blocks
  193. || !s->frame_blocks) {
  194. av_log(avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  195. cleanup(s);
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. static int new_key_frame(FlashSV2Context * s)
  201. {
  202. int i;
  203. memcpy(s->key_blocks, s->frame_blocks, s->blocks_size);
  204. memcpy(s->key_frame, s->current_frame, s->frame_size);
  205. for (i = 0; i < s->rows * s->cols; i++) {
  206. s->key_blocks[i].enc += (s->keybuffer - s->encbuffer);
  207. s->key_blocks[i].sl_begin = 0;
  208. s->key_blocks[i].sl_end = 0;
  209. s->key_blocks[i].data = 0;
  210. }
  211. FFSWAP(uint8_t * , s->keybuffer, s->encbuffer);
  212. return 0;
  213. }
  214. static int write_palette(FlashSV2Context * s, uint8_t * buf, int buf_size)
  215. {
  216. //this isn't implemented yet! Default palette only!
  217. return -1;
  218. }
  219. static int write_header(FlashSV2Context * s, uint8_t * buf, int buf_size)
  220. {
  221. PutBitContext pb;
  222. int buf_pos, len;
  223. if (buf_size < 5)
  224. return -1;
  225. init_put_bits(&pb, buf, buf_size * 8);
  226. put_bits(&pb, 4, (s->block_width >> 4) - 1);
  227. put_bits(&pb, 12, s->image_width);
  228. put_bits(&pb, 4, (s->block_height >> 4) - 1);
  229. put_bits(&pb, 12, s->image_height);
  230. flush_put_bits(&pb);
  231. buf_pos = 4;
  232. buf[buf_pos++] = s->flags;
  233. if (s->flags & HAS_PALLET_INFO) {
  234. len = write_palette(s, buf + buf_pos, buf_size - buf_pos);
  235. if (len < 0)
  236. return -1;
  237. buf_pos += len;
  238. }
  239. return buf_pos;
  240. }
  241. static int write_block(Block * b, uint8_t * buf, int buf_size)
  242. {
  243. int buf_pos = 0;
  244. unsigned block_size = b->data_size;
  245. if (b->flags & HAS_DIFF_BLOCKS)
  246. block_size += 2;
  247. if (b->flags & ZLIB_PRIME_COMPRESS_CURRENT)
  248. block_size += 2;
  249. if (block_size > 0)
  250. block_size += 1;
  251. if (buf_size < block_size + 2)
  252. return -1;
  253. buf[buf_pos++] = block_size >> 8;
  254. buf[buf_pos++] = block_size;
  255. if (block_size == 0)
  256. return buf_pos;
  257. buf[buf_pos++] = b->flags;
  258. if (b->flags & HAS_DIFF_BLOCKS) {
  259. buf[buf_pos++] = (b->start);
  260. buf[buf_pos++] = (b->len);
  261. }
  262. if (b->flags & ZLIB_PRIME_COMPRESS_CURRENT) {
  263. //This feature of the format is poorly understood, and as of now, unused.
  264. buf[buf_pos++] = (b->col);
  265. buf[buf_pos++] = (b->row);
  266. }
  267. memcpy(buf + buf_pos, b->data, b->data_size);
  268. buf_pos += b->data_size;
  269. return buf_pos;
  270. }
  271. static int encode_zlib(Block * b, uint8_t * buf, unsigned long *buf_size, int comp)
  272. {
  273. int res = compress2(buf, buf_size, b->sl_begin, b->sl_end - b->sl_begin, comp);
  274. return res == Z_OK ? 0 : -1;
  275. }
  276. static int encode_zlibprime(Block * b, Block * prime, uint8_t * buf,
  277. int *buf_size, int comp)
  278. {
  279. z_stream s;
  280. int res;
  281. s.zalloc = NULL;
  282. s.zfree = NULL;
  283. s.opaque = NULL;
  284. res = deflateInit(&s, comp);
  285. if (res < 0)
  286. return -1;
  287. s.next_in = prime->enc;
  288. s.avail_in = prime->enc_size;
  289. while (s.avail_in > 0) {
  290. s.next_out = buf;
  291. s.avail_out = *buf_size;
  292. res = deflate(&s, Z_SYNC_FLUSH);
  293. if (res < 0)
  294. return -1;
  295. }
  296. s.next_in = b->sl_begin;
  297. s.avail_in = b->sl_end - b->sl_begin;
  298. s.next_out = buf;
  299. s.avail_out = *buf_size;
  300. res = deflate(&s, Z_FINISH);
  301. deflateEnd(&s);
  302. *buf_size -= s.avail_out;
  303. if (res != Z_STREAM_END)
  304. return -1;
  305. return 0;
  306. }
  307. static int encode_bgr(Block * b, const uint8_t * src, int stride)
  308. {
  309. int i;
  310. uint8_t *ptr = b->enc;
  311. for (i = 0; i < b->start; i++)
  312. memcpy(ptr + i * b->width * 3, src + i * stride, b->width * 3);
  313. b->sl_begin = ptr + i * b->width * 3;
  314. for (; i < b->start + b->len; i++)
  315. memcpy(ptr + i * b->width * 3, src + i * stride, b->width * 3);
  316. b->sl_end = ptr + i * b->width * 3;
  317. for (; i < b->height; i++)
  318. memcpy(ptr + i * b->width * 3, src + i * stride, b->width * 3);
  319. b->enc_size = ptr + i * b->width * 3 - b->enc;
  320. return b->enc_size;
  321. }
  322. static inline unsigned pixel_color15(const uint8_t * src)
  323. {
  324. return (src[0] >> 3) | ((src[1] & 0xf8) << 2) | ((src[2] & 0xf8) << 7);
  325. }
  326. static inline unsigned int chroma_diff(unsigned int c1, unsigned int c2)
  327. {
  328. unsigned int t1 = (c1 & 0x000000ff) + ((c1 & 0x0000ff00) >> 8) + ((c1 & 0x00ff0000) >> 16);
  329. unsigned int t2 = (c2 & 0x000000ff) + ((c2 & 0x0000ff00) >> 8) + ((c2 & 0x00ff0000) >> 16);
  330. return abs(t1 - t2) + abs((c1 & 0x000000ff) - (c2 & 0x000000ff)) +
  331. abs(((c1 & 0x0000ff00) >> 8) - ((c2 & 0x0000ff00) >> 8)) +
  332. abs(((c1 & 0x00ff0000) >> 16) - ((c2 & 0x00ff0000) >> 16));
  333. }
  334. static inline int pixel_color7_fast(Palette * palette, unsigned c15)
  335. {
  336. return palette->index[c15];
  337. }
  338. static int pixel_color7_slow(Palette * palette, unsigned color)
  339. {
  340. int i, min = 0x7fffffff;
  341. int minc = -1;
  342. for (i = 0; i < 128; i++) {
  343. int c1 = palette->colors[i];
  344. int diff = chroma_diff(c1, color);
  345. if (diff < min) {
  346. min = diff;
  347. minc = i;
  348. }
  349. }
  350. return minc;
  351. }
  352. static inline unsigned pixel_bgr(const uint8_t * src)
  353. {
  354. return (src[0]) | (src[1] << 8) | (src[2] << 16);
  355. }
  356. static int write_pixel_15_7(Palette * palette, uint8_t * dest, const uint8_t * src,
  357. int dist)
  358. {
  359. unsigned c15 = pixel_color15(src);
  360. unsigned color = pixel_bgr(src);
  361. int d15 = chroma_diff(color, color & 0x00f8f8f8);
  362. int c7 = pixel_color7_fast(palette, c15);
  363. int d7 = chroma_diff(color, palette->colors[c7]);
  364. if (dist + d15 >= d7) {
  365. dest[0] = c7;
  366. return 1;
  367. } else {
  368. dest[0] = 0x80 | (c15 >> 8);
  369. dest[1] = c15 & 0xff;
  370. return 2;
  371. }
  372. }
  373. static int update_palette_index(Palette * palette)
  374. {
  375. int r, g, b;
  376. unsigned int bgr, c15, index;
  377. for (r = 4; r < 256; r += 8) {
  378. for (g = 4; g < 256; g += 8) {
  379. for (b = 4; b < 256; b += 8) {
  380. bgr = b | (g << 8) | (r << 16);
  381. c15 = (b >> 3) | ((g & 0xf8) << 2) | ((r & 0xf8) << 7);
  382. index = pixel_color7_slow(palette, bgr);
  383. palette->index[c15] = index;
  384. }
  385. }
  386. }
  387. return 0;
  388. }
  389. static const unsigned int default_screen_video_v2_palette[128] = {
  390. 0x00000000, 0x00333333, 0x00666666, 0x00999999, 0x00CCCCCC, 0x00FFFFFF,
  391. 0x00330000, 0x00660000, 0x00990000, 0x00CC0000, 0x00FF0000, 0x00003300,
  392. 0x00006600, 0x00009900, 0x0000CC00, 0x0000FF00, 0x00000033, 0x00000066,
  393. 0x00000099, 0x000000CC, 0x000000FF, 0x00333300, 0x00666600, 0x00999900,
  394. 0x00CCCC00, 0x00FFFF00, 0x00003333, 0x00006666, 0x00009999, 0x0000CCCC,
  395. 0x0000FFFF, 0x00330033, 0x00660066, 0x00990099, 0x00CC00CC, 0x00FF00FF,
  396. 0x00FFFF33, 0x00FFFF66, 0x00FFFF99, 0x00FFFFCC, 0x00FF33FF, 0x00FF66FF,
  397. 0x00FF99FF, 0x00FFCCFF, 0x0033FFFF, 0x0066FFFF, 0x0099FFFF, 0x00CCFFFF,
  398. 0x00CCCC33, 0x00CCCC66, 0x00CCCC99, 0x00CCCCFF, 0x00CC33CC, 0x00CC66CC,
  399. 0x00CC99CC, 0x00CCFFCC, 0x0033CCCC, 0x0066CCCC, 0x0099CCCC, 0x00FFCCCC,
  400. 0x00999933, 0x00999966, 0x009999CC, 0x009999FF, 0x00993399, 0x00996699,
  401. 0x0099CC99, 0x0099FF99, 0x00339999, 0x00669999, 0x00CC9999, 0x00FF9999,
  402. 0x00666633, 0x00666699, 0x006666CC, 0x006666FF, 0x00663366, 0x00669966,
  403. 0x0066CC66, 0x0066FF66, 0x00336666, 0x00996666, 0x00CC6666, 0x00FF6666,
  404. 0x00333366, 0x00333399, 0x003333CC, 0x003333FF, 0x00336633, 0x00339933,
  405. 0x0033CC33, 0x0033FF33, 0x00663333, 0x00993333, 0x00CC3333, 0x00FF3333,
  406. 0x00003366, 0x00336600, 0x00660033, 0x00006633, 0x00330066, 0x00663300,
  407. 0x00336699, 0x00669933, 0x00993366, 0x00339966, 0x00663399, 0x00996633,
  408. 0x006699CC, 0x0099CC66, 0x00CC6699, 0x0066CC99, 0x009966CC, 0x00CC9966,
  409. 0x0099CCFF, 0x00CCFF99, 0x00FF99CC, 0x0099FFCC, 0x00CC99FF, 0x00FFCC99,
  410. 0x00111111, 0x00222222, 0x00444444, 0x00555555, 0x00AAAAAA, 0x00BBBBBB,
  411. 0x00DDDDDD, 0x00EEEEEE
  412. };
  413. static int generate_default_palette(Palette * palette)
  414. {
  415. memcpy(palette->colors, default_screen_video_v2_palette,
  416. sizeof(default_screen_video_v2_palette));
  417. return update_palette_index(palette);
  418. }
  419. static int generate_optimum_palette(Palette * palette, const uint8_t * image,
  420. int width, int height, int stride)
  421. {
  422. //this isn't implemented yet! Default palette only!
  423. return -1;
  424. }
  425. static inline int encode_15_7_sl(Palette * palette, uint8_t * dest,
  426. const uint8_t * src, int width, int dist)
  427. {
  428. int len = 0, x;
  429. for (x = 0; x < width; x++) {
  430. len += write_pixel_15_7(palette, dest + len, src + 3 * x, dist);
  431. }
  432. return len;
  433. }
  434. static int encode_15_7(Palette * palette, Block * b, const uint8_t * src,
  435. int stride, int dist)
  436. {
  437. int i;
  438. uint8_t *ptr = b->enc;
  439. for (i = 0; i < b->start; i++)
  440. ptr += encode_15_7_sl(palette, ptr, src + i * stride, b->width, dist);
  441. b->sl_begin = ptr;
  442. for (; i < b->start + b->len; i++)
  443. ptr += encode_15_7_sl(palette, ptr, src + i * stride, b->width, dist);
  444. b->sl_end = ptr;
  445. for (; i < b->height; i++)
  446. ptr += encode_15_7_sl(palette, ptr, src + i * stride, b->width, dist);
  447. b->enc_size = ptr - b->enc;
  448. return b->enc_size;
  449. }
  450. static int encode_block(Palette * palette, Block * b, Block * prev,
  451. const uint8_t * src, int stride, int comp, int dist,
  452. int keyframe)
  453. {
  454. unsigned buf_size = b->width * b->height * 6;
  455. uint8_t buf[buf_size];
  456. int res;
  457. if (b->flags & COLORSPACE_15_7) {
  458. encode_15_7(palette, b, src, stride, dist);
  459. } else {
  460. encode_bgr(b, src, stride);
  461. }
  462. if (b->len > 0) {
  463. b->data_size = buf_size;
  464. res = encode_zlib(b, b->data, &b->data_size, comp);
  465. if (res)
  466. return res;
  467. if (!keyframe) {
  468. res = encode_zlibprime(b, prev, buf, &buf_size, comp);
  469. if (res)
  470. return res;
  471. if (buf_size < b->data_size) {
  472. b->data_size = buf_size;
  473. memcpy(b->data, buf, buf_size);
  474. b->flags |= ZLIB_PRIME_COMPRESS_PREVIOUS;
  475. }
  476. }
  477. } else {
  478. b->data_size = 0;
  479. }
  480. return 0;
  481. }
  482. static int compare_sl(FlashSV2Context * s, Block * b, const uint8_t * src,
  483. uint8_t * frame, uint8_t * key, int y, int keyframe)
  484. {
  485. if (memcmp(src, frame, b->width * 3) != 0) {
  486. b->dirty = 1;
  487. memcpy(frame, src, b->width * 3);
  488. #ifndef FLASHSV2_DUMB
  489. s->diff_lines++;
  490. #endif
  491. }
  492. if (memcmp(src, key, b->width * 3) != 0) {
  493. if (b->len == 0)
  494. b->start = y;
  495. b->len = y + 1 - b->start;
  496. }
  497. return 0;
  498. }
  499. static int mark_all_blocks(FlashSV2Context * s, const uint8_t * src, int stride,
  500. int keyframe)
  501. {
  502. int sl, rsl, col, pos, possl;
  503. Block *b;
  504. for (sl = s->image_height - 1; sl >= 0; sl--) {
  505. for (col = 0; col < s->cols; col++) {
  506. rsl = s->image_height - sl - 1;
  507. b = s->frame_blocks + col + rsl / s->block_height * s->cols;
  508. possl = stride * sl + col * s->block_width * 3;
  509. pos = s->image_width * rsl * 3 + col * s->block_width * 3;
  510. compare_sl(s, b, src + possl, s->current_frame + pos,
  511. s->key_frame + pos, rsl % s->block_height, keyframe);
  512. }
  513. }
  514. #ifndef FLASHSV2_DUMB
  515. s->tot_lines += s->image_height * s->cols;
  516. #endif
  517. return 0;
  518. }
  519. static int encode_all_blocks(FlashSV2Context * s, int keyframe)
  520. {
  521. int row, col, res;
  522. uint8_t *data;
  523. Block *b, *prev;
  524. for (row = 0; row < s->rows; row++) {
  525. for (col = 0; col < s->cols; col++) {
  526. b = s->frame_blocks + (row * s->cols + col);
  527. prev = s->key_blocks + (row * s->cols + col);
  528. if (keyframe) {
  529. b->start = 0;
  530. b->len = b->height;
  531. b->flags = s->use15_7 ? COLORSPACE_15_7 : 0;
  532. } else if (!b->dirty) {
  533. b->start = 0;
  534. b->len = 0;
  535. b->data_size = 0;
  536. b->flags = s->use15_7 ? COLORSPACE_15_7 : 0;
  537. continue;
  538. } else {
  539. b->flags = s->use15_7 ? COLORSPACE_15_7 | HAS_DIFF_BLOCKS : HAS_DIFF_BLOCKS;
  540. }
  541. data = s->current_frame + s->image_width * 3 * s->block_height * row + s->block_width * col * 3;
  542. res = encode_block(&s->palette, b, prev, data, s->image_width * 3, s->comp, s->dist, keyframe);
  543. #ifndef FLASHSV2_DUMB
  544. if (b->dirty)
  545. s->diff_blocks++;
  546. s->comp_size += b->data_size;
  547. s->uncomp_size += b->enc_size;
  548. #endif
  549. if (res)
  550. return res;
  551. }
  552. }
  553. #ifndef FLASHSV2_DUMB
  554. s->raw_size += s->image_width * s->image_height * 3;
  555. s->tot_blocks += s->rows * s->cols;
  556. #endif
  557. return 0;
  558. }
  559. static int write_all_blocks(FlashSV2Context * s, uint8_t * buf,
  560. int buf_size)
  561. {
  562. int row, col, buf_pos = 0, len;
  563. Block *b;
  564. for (row = 0; row < s->rows; row++) {
  565. for (col = 0; col < s->cols; col++) {
  566. b = s->frame_blocks + row * s->cols + col;
  567. len = write_block(b, buf + buf_pos, buf_size - buf_pos);
  568. b->start = b->len = b->dirty = 0;
  569. if (len < 0)
  570. return len;
  571. buf_pos += len;
  572. }
  573. }
  574. return buf_pos;
  575. }
  576. static int write_bitstream(FlashSV2Context * s, const uint8_t * src, int stride,
  577. uint8_t * buf, int buf_size, int keyframe)
  578. {
  579. int buf_pos, res;
  580. res = mark_all_blocks(s, src, stride, keyframe);
  581. if (res)
  582. return res;
  583. res = encode_all_blocks(s, keyframe);
  584. if (res)
  585. return res;
  586. res = write_header(s, buf, buf_size);
  587. if (res < 0) {
  588. return res;
  589. } else {
  590. buf_pos = res;
  591. }
  592. res = write_all_blocks(s, buf + buf_pos, buf_size - buf_pos);
  593. if (res < 0)
  594. return res;
  595. buf_pos += res;
  596. #ifndef FLASHSV2_DUMB
  597. s->total_bits += ((double) buf_pos) * 8.0;
  598. #endif
  599. return buf_pos;
  600. }
  601. static void recommend_keyframe(FlashSV2Context * s, int *keyframe)
  602. {
  603. #ifndef FLASHSV2_DUMB
  604. double block_ratio, line_ratio, enc_ratio, comp_ratio, data_ratio;
  605. if (s->avctx->gop_size > 0) {
  606. block_ratio = s->diff_blocks / s->tot_blocks;
  607. line_ratio = s->diff_lines / s->tot_lines;
  608. enc_ratio = s->uncomp_size / s->raw_size;
  609. comp_ratio = s->comp_size / s->uncomp_size;
  610. data_ratio = s->comp_size / s->raw_size;
  611. if ((block_ratio >= 0.5 && line_ratio / block_ratio <= 0.5) || line_ratio >= 0.95) {
  612. *keyframe = 1;
  613. return;
  614. }
  615. }
  616. #else
  617. return;
  618. #endif
  619. }
  620. static const double block_size_fraction = 1.0 / 300;
  621. static int optimum_block_width(FlashSV2Context * s)
  622. {
  623. #ifndef FLASHSV2_DUMB
  624. double save = (1-pow(s->diff_lines/s->diff_blocks/s->block_height, 0.5)) * s->comp_size/s->tot_blocks;
  625. double width = block_size_fraction * sqrt(0.5 * save * s->rows * s->cols) * s->image_width;
  626. int pwidth = ((int) width);
  627. return FFCLIP(pwidth & ~15, 256, 16);
  628. #else
  629. return 64;
  630. #endif
  631. }
  632. static int optimum_block_height(FlashSV2Context * s)
  633. {
  634. #ifndef FLASHSV2_DUMB
  635. double save = (1-pow(s->diff_lines/s->diff_blocks/s->block_height, 0.5)) * s->comp_size/s->tot_blocks;
  636. double height = block_size_fraction * sqrt(0.5 * save * s->rows * s->cols) * s->image_height;
  637. int pheight = ((int) height);
  638. return FFCLIP(pheight & ~15, 256, 16);
  639. #else
  640. return 64;
  641. #endif
  642. }
  643. static const double use15_7_threshold = 8192;
  644. static int optimum_use15_7(FlashSV2Context * s)
  645. {
  646. #ifndef FLASHSV2_DUMB
  647. double ideal = ((double)(s->avctx->bit_rate * s->avctx->time_base.den * s->avctx->ticks_per_frame)) /
  648. ((double) s->avctx->time_base.num) * s->avctx->frame_number;
  649. if (ideal + use15_7_threshold < s->total_bits) {
  650. return 1;
  651. } else {
  652. return 0;
  653. }
  654. #else
  655. return s->avctx->global_quality == 0;
  656. #endif
  657. }
  658. static const double color15_7_factor = 100;
  659. static int optimum_dist(FlashSV2Context * s)
  660. {
  661. #ifndef FLASHSV2_DUMB
  662. double ideal =
  663. s->avctx->bit_rate * s->avctx->time_base.den *
  664. s->avctx->ticks_per_frame;
  665. int dist = pow((s->total_bits / ideal) * color15_7_factor, 3);
  666. av_log(s->avctx, AV_LOG_DEBUG, "dist: %d\n", dist);
  667. return dist;
  668. #else
  669. return 15;
  670. #endif
  671. }
  672. static int reconfigure_at_keyframe(FlashSV2Context * s, const uint8_t * image,
  673. int stride)
  674. {
  675. int update_palette = 0;
  676. int res;
  677. s->block_width = optimum_block_width(s);
  678. s->block_height = optimum_block_height(s);
  679. s->rows = (s->image_height + s->block_height - 1) / s->block_height;
  680. s->cols = (s->image_width + s->block_width - 1) / s->block_width;
  681. if (s->rows * s->cols != s->blocks_size / sizeof(Block)) {
  682. if (s->rows * s->cols > s->blocks_size / sizeof(Block)) {
  683. s->frame_blocks = av_realloc(s->frame_blocks, s->rows * s->cols * sizeof(Block));
  684. s->key_blocks = av_realloc(s->key_blocks, s->cols * s->rows * sizeof(Block));
  685. if (!s->frame_blocks || !s->key_blocks) {
  686. av_log(s->avctx, AV_LOG_ERROR, "Memory allocation failed.\n");
  687. return -1;
  688. }
  689. s->blocks_size = s->rows * s->cols * sizeof(Block);
  690. }
  691. init_blocks(s, s->frame_blocks, s->encbuffer, s->databuffer);
  692. init_blocks(s, s->key_blocks, s->keybuffer, 0);
  693. }
  694. s->use15_7 = optimum_use15_7(s);
  695. if (s->use15_7) {
  696. if ((s->use_custom_palette && s->palette_type != 1) || update_palette) {
  697. res = generate_optimum_palette(&s->palette, image, s->image_width, s->image_height, stride);
  698. if (res)
  699. return res;
  700. s->palette_type = 1;
  701. av_log(s->avctx, AV_LOG_DEBUG, "Generated optimum palette\n");
  702. } else if (!s->use_custom_palette && s->palette_type != 0) {
  703. res = generate_default_palette(&s->palette);
  704. if (res)
  705. return res;
  706. s->palette_type = 0;
  707. av_log(s->avctx, AV_LOG_DEBUG, "Generated default palette\n");
  708. }
  709. }
  710. reset_stats(s);
  711. return 0;
  712. }
  713. static int flashsv2_encode_frame(AVCodecContext * avctx, uint8_t * buf,
  714. int buf_size, void *data)
  715. {
  716. FlashSV2Context *const s = avctx->priv_data;
  717. AVFrame *pict = data;
  718. AVFrame *const p = &s->frame;
  719. int res;
  720. int keyframe = 0;
  721. *p = *pict;
  722. /* First frame needs to be a keyframe */
  723. if (avctx->frame_number == 0)
  724. keyframe = 1;
  725. /* Check the placement of keyframes */
  726. if (avctx->gop_size > 0) {
  727. if (avctx->frame_number >= s->last_key_frame + avctx->gop_size)
  728. keyframe = 1;
  729. }
  730. if (buf_size < s->frame_size) {
  731. //Conservative upper bound check for compressed data
  732. av_log(avctx, AV_LOG_ERROR, "buf_size %d < %d\n", buf_size, s->frame_size);
  733. return -1;
  734. }
  735. if (!keyframe
  736. && avctx->frame_number > s->last_key_frame + avctx->keyint_min) {
  737. recommend_keyframe(s, &keyframe);
  738. if (keyframe)
  739. av_log(avctx, AV_LOG_DEBUG, "Recommending key frame at frame %d\n", avctx->frame_number);
  740. }
  741. if (keyframe) {
  742. res = reconfigure_at_keyframe(s, p->data[0], p->linesize[0]);
  743. if (res)
  744. return res;
  745. }
  746. if (s->use15_7)
  747. s->dist = optimum_dist(s);
  748. res = write_bitstream(s, p->data[0], p->linesize[0], buf, buf_size, keyframe);
  749. if (keyframe) {
  750. new_key_frame(s);
  751. p->pict_type = FF_I_TYPE;
  752. p->key_frame = 1;
  753. s->last_key_frame = avctx->frame_number;
  754. av_log(avctx, AV_LOG_DEBUG, "Inserting key frame at frame %d\n", avctx->frame_number);
  755. } else {
  756. p->pict_type = FF_P_TYPE;
  757. p->key_frame = 0;
  758. }
  759. avctx->coded_frame = p;
  760. return res;
  761. }
  762. static av_cold int flashsv2_encode_end(AVCodecContext * avctx)
  763. {
  764. FlashSV2Context *s = avctx->priv_data;
  765. cleanup(s);
  766. return 0;
  767. }
  768. AVCodec ff_flashsv2_encoder = {
  769. "flashsv2",
  770. AVMEDIA_TYPE_VIDEO,
  771. CODEC_ID_FLASHSV2,
  772. sizeof(FlashSV2Context),
  773. flashsv2_encode_init,
  774. flashsv2_encode_frame,
  775. flashsv2_encode_end,
  776. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_BGR24, PIX_FMT_NONE},
  777. .long_name = NULL_IF_CONFIG_SMALL("Flash Screen Video Version 2"),
  778. .capabilities = CODEC_CAP_EXPERIMENTAL,
  779. };