a64multienc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * a64 video encoder - multicolor modes
  3. * Copyright (c) 2009 Tobias Bindhammer
  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. * a64 video encoder - multicolor modes
  24. */
  25. #include "a64enc.h"
  26. #include "a64colors.h"
  27. #include "a64tables.h"
  28. #include "elbg.h"
  29. #include "libavutil/intreadwrite.h"
  30. #define DITHERSTEPS 8
  31. #define CHARSET_CHARS 256
  32. #define INTERLACED 1
  33. #define CROP_SCREENS 1
  34. /* gray gradient */
  35. static const int mc_colors[5]={0x0,0xb,0xc,0xf,0x1};
  36. /* other possible gradients - to be tested */
  37. //static const int mc_colors[5]={0x0,0x8,0xa,0xf,0x7};
  38. //static const int mc_colors[5]={0x0,0x9,0x8,0xa,0x3};
  39. static void to_meta_with_crop(AVCodecContext *avctx, AVFrame *p, int *dest)
  40. {
  41. int blockx, blocky, x, y;
  42. int luma = 0;
  43. int height = FFMIN(avctx->height, C64YRES);
  44. int width = FFMIN(avctx->width , C64XRES);
  45. uint8_t *src = p->data[0];
  46. for (blocky = 0; blocky < C64YRES; blocky += 8) {
  47. for (blockx = 0; blockx < C64XRES; blockx += 8) {
  48. for (y = blocky; y < blocky + 8 && y < C64YRES; y++) {
  49. for (x = blockx; x < blockx + 8 && x < C64XRES; x += 2) {
  50. if(x < width && y < height) {
  51. /* build average over 2 pixels */
  52. luma = (src[(x + 0 + y * p->linesize[0])] +
  53. src[(x + 1 + y * p->linesize[0])]) / 2;
  54. /* write blocks as linear data now so they are suitable for elbg */
  55. dest[0] = luma;
  56. }
  57. dest++;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. static void render_charset(AVCodecContext *avctx, uint8_t *charset,
  64. uint8_t *colrammap)
  65. {
  66. A64Context *c = avctx->priv_data;
  67. uint8_t row1, row2;
  68. int charpos, x, y;
  69. int a, b;
  70. uint8_t pix;
  71. int lowdiff, highdiff;
  72. int *best_cb = c->mc_best_cb;
  73. static uint8_t index1[256];
  74. static uint8_t index2[256];
  75. static uint8_t dither[256];
  76. int i;
  77. int distance;
  78. /* generate lookup-tables for dither and index before looping */
  79. i = 0;
  80. for (a=0; a < 256; a++) {
  81. if(i < c->mc_pal_size -1 && a == c->mc_luma_vals[i + 1]) {
  82. distance = c->mc_luma_vals[i + 1] - c->mc_luma_vals[i];
  83. for(b = 0; b <= distance; b++) {
  84. dither[c->mc_luma_vals[i] + b] = b * (DITHERSTEPS - 1) / distance;
  85. }
  86. i++;
  87. }
  88. if(i >= c->mc_pal_size - 1) dither[a] = 0;
  89. index1[a] = i;
  90. index2[a] = FFMIN(i + 1, c->mc_pal_size - 1);
  91. }
  92. /* and render charset */
  93. for (charpos = 0; charpos < CHARSET_CHARS; charpos++) {
  94. lowdiff = 0;
  95. highdiff = 0;
  96. for (y = 0; y < 8; y++) {
  97. row1 = 0; row2 = 0;
  98. for (x = 0; x < 4; x++) {
  99. pix = best_cb[y * 4 + x];
  100. /* accumulate error for brightest/darkest color */
  101. if (index1[pix] >= 3)
  102. highdiff += pix - c->mc_luma_vals[3];
  103. if (index1[pix] < 1)
  104. lowdiff += c->mc_luma_vals[1] - pix;
  105. row1 <<= 2;
  106. if (INTERLACED) {
  107. row2 <<= 2;
  108. if (interlaced_dither_patterns[dither[pix]][(y & 3) * 2 + 0][x & 3])
  109. row1 |= 3-(index2[pix] & 3);
  110. else
  111. row1 |= 3-(index1[pix] & 3);
  112. if (interlaced_dither_patterns[dither[pix]][(y & 3) * 2 + 1][x & 3])
  113. row2 |= 3-(index2[pix] & 3);
  114. else
  115. row2 |= 3-(index1[pix] & 3);
  116. }
  117. else {
  118. if (multi_dither_patterns[dither[pix]][(y & 3)][x & 3])
  119. row1 |= 3-(index2[pix] & 3);
  120. else
  121. row1 |= 3-(index1[pix] & 3);
  122. }
  123. }
  124. charset[y+0x000] = row1;
  125. if (INTERLACED) charset[y+0x800] = row2;
  126. }
  127. /* do we need to adjust pixels? */
  128. if (highdiff > 0 && lowdiff > 0 && c->mc_use_5col) {
  129. if (lowdiff > highdiff) {
  130. for (x = 0; x < 32; x++)
  131. best_cb[x] = FFMIN(c->mc_luma_vals[3], best_cb[x]);
  132. } else {
  133. for (x = 0; x < 32; x++)
  134. best_cb[x] = FFMAX(c->mc_luma_vals[1], best_cb[x]);
  135. }
  136. charpos--; /* redo now adjusted char */
  137. /* no adjustment needed, all fine */
  138. } else {
  139. /* advance pointers */
  140. best_cb += 32;
  141. charset += 8;
  142. /* remember colorram value */
  143. colrammap[charpos] = (highdiff > 0);
  144. }
  145. }
  146. }
  147. static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
  148. {
  149. A64Context *c = avctx->priv_data;
  150. av_free(c->mc_meta_charset);
  151. av_free(c->mc_best_cb);
  152. av_free(c->mc_charset);
  153. av_free(c->mc_charmap);
  154. av_free(c->mc_colram);
  155. return 0;
  156. }
  157. static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
  158. {
  159. A64Context *c = avctx->priv_data;
  160. int a;
  161. av_lfg_init(&c->randctx, 1);
  162. if (avctx->global_quality < 1) {
  163. c->mc_lifetime = 4;
  164. } else {
  165. c->mc_lifetime = avctx->global_quality /= FF_QP2LAMBDA;
  166. }
  167. av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime);
  168. c->mc_frame_counter = 0;
  169. c->mc_use_5col = avctx->codec->id == CODEC_ID_A64_MULTI5;
  170. c->mc_pal_size = 4 + c->mc_use_5col;
  171. /* precalc luma values for later use */
  172. for (a = 0; a < c->mc_pal_size; a++) {
  173. c->mc_luma_vals[a]=a64_palette[mc_colors[a]][0] * 0.30 +
  174. a64_palette[mc_colors[a]][1] * 0.59 +
  175. a64_palette[mc_colors[a]][2] * 0.11;
  176. }
  177. if (!(c->mc_meta_charset = av_malloc(32000 * c->mc_lifetime * sizeof(int))) ||
  178. !(c->mc_best_cb = av_malloc(CHARSET_CHARS * 32 * sizeof(int))) ||
  179. !(c->mc_charmap = av_mallocz(1000 * c->mc_lifetime * sizeof(int))) ||
  180. !(c->mc_colram = av_mallocz(CHARSET_CHARS * sizeof(uint8_t))) ||
  181. !(c->mc_charset = av_malloc(0x800 * (INTERLACED+1) * sizeof(uint8_t)))) {
  182. av_log(avctx, AV_LOG_ERROR, "Failed to allocate buffer memory.\n");
  183. return AVERROR(ENOMEM);
  184. }
  185. /* set up extradata */
  186. if (!(avctx->extradata = av_mallocz(8 * 4 + FF_INPUT_BUFFER_PADDING_SIZE))) {
  187. av_log(avctx, AV_LOG_ERROR, "Failed to allocate memory for extradata.\n");
  188. return AVERROR(ENOMEM);
  189. }
  190. avctx->extradata_size = 8 * 4;
  191. AV_WB32(avctx->extradata, c->mc_lifetime);
  192. AV_WB32(avctx->extradata + 16, INTERLACED);
  193. avcodec_get_frame_defaults(&c->picture);
  194. avctx->coded_frame = &c->picture;
  195. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  196. avctx->coded_frame->key_frame = 1;
  197. if (!avctx->codec_tag)
  198. avctx->codec_tag = AV_RL32("a64m");
  199. return 0;
  200. }
  201. static void a64_compress_colram(unsigned char *buf, int *charmap, uint8_t *colram)
  202. {
  203. int a;
  204. uint8_t temp;
  205. /* only needs to be done in 5col mode */
  206. /* XXX could be squeezed to 0x80 bytes */
  207. for (a = 0; a < 256; a++) {
  208. temp = colram[charmap[a + 0x000]] << 0;
  209. temp |= colram[charmap[a + 0x100]] << 1;
  210. temp |= colram[charmap[a + 0x200]] << 2;
  211. if (a < 0xe8) temp |= colram[charmap[a + 0x300]] << 3;
  212. buf[a] = temp << 2;
  213. }
  214. }
  215. static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  216. int buf_size, void *data)
  217. {
  218. A64Context *c = avctx->priv_data;
  219. AVFrame *pict = data;
  220. AVFrame *const p = (AVFrame *) & c->picture;
  221. int frame;
  222. int x, y;
  223. int b_height;
  224. int b_width;
  225. int req_size;
  226. int num_frames = c->mc_lifetime;
  227. int *charmap = c->mc_charmap;
  228. uint8_t *colram = c->mc_colram;
  229. uint8_t *charset = c->mc_charset;
  230. int *meta = c->mc_meta_charset;
  231. int *best_cb = c->mc_best_cb;
  232. int charset_size = 0x800 * (INTERLACED + 1);
  233. int colram_size = 0x100 * c->mc_use_5col;
  234. int screen_size;
  235. if(CROP_SCREENS) {
  236. b_height = FFMIN(avctx->height,C64YRES) >> 3;
  237. b_width = FFMIN(avctx->width ,C64XRES) >> 3;
  238. screen_size = b_width * b_height;
  239. } else {
  240. b_height = C64YRES >> 3;
  241. b_width = C64XRES >> 3;
  242. screen_size = 0x400;
  243. }
  244. /* no data, means end encoding asap */
  245. if (!data) {
  246. /* all done, end encoding */
  247. if (!c->mc_lifetime) return 0;
  248. /* no more frames in queue, prepare to flush remaining frames */
  249. if (!c->mc_frame_counter) {
  250. num_frames = c->mc_lifetime;
  251. c->mc_lifetime = 0;
  252. }
  253. /* still frames in queue so limit lifetime to remaining frames */
  254. else c->mc_lifetime = c->mc_frame_counter;
  255. /* still new data available */
  256. } else {
  257. /* fill up mc_meta_charset with data until lifetime exceeds */
  258. if (c->mc_frame_counter < c->mc_lifetime) {
  259. *p = *pict;
  260. p->pict_type = AV_PICTURE_TYPE_I;
  261. p->key_frame = 1;
  262. to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
  263. c->mc_frame_counter++;
  264. /* lifetime is not reached so wait for next frame first */
  265. return 0;
  266. }
  267. }
  268. /* lifetime reached so now convert X frames at once */
  269. if (c->mc_frame_counter == c->mc_lifetime) {
  270. req_size = 0;
  271. /* any frames to encode? */
  272. if (c->mc_lifetime) {
  273. /* calc optimal new charset + charmaps */
  274. ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  275. ff_do_elbg (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  276. /* create colorram map and a c64 readable charset */
  277. render_charset(avctx, charset, colram);
  278. /* copy charset to buf */
  279. memcpy(buf,charset, charset_size);
  280. /* advance pointers */
  281. buf += charset_size;
  282. charset += charset_size;
  283. req_size += charset_size;
  284. }
  285. /* no charset so clean buf */
  286. else memset(buf, 0, charset_size);
  287. /* write x frames to buf */
  288. for (frame = 0; frame < c->mc_lifetime; frame++) {
  289. /* copy charmap to buf. buf is uchar*, charmap is int*, so no memcpy here, sorry */
  290. for (y = 0; y < b_height; y++) {
  291. for (x = 0; x < b_width; x++) {
  292. buf[y * b_width + x] = charmap[y * b_width + x];
  293. }
  294. }
  295. /* advance pointers */
  296. buf += screen_size;
  297. req_size += screen_size;
  298. /* compress and copy colram to buf */
  299. if (c->mc_use_5col) {
  300. a64_compress_colram(buf, charmap, colram);
  301. /* advance pointers */
  302. buf += colram_size;
  303. req_size += colram_size;
  304. }
  305. /* advance to next charmap */
  306. charmap += 1000;
  307. }
  308. AV_WB32(avctx->extradata + 4, c->mc_frame_counter);
  309. AV_WB32(avctx->extradata + 8, charset_size);
  310. AV_WB32(avctx->extradata + 12, screen_size + colram_size);
  311. /* reset counter */
  312. c->mc_frame_counter = 0;
  313. if (req_size > buf_size) {
  314. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
  315. return -1;
  316. }
  317. return req_size;
  318. }
  319. return 0;
  320. }
  321. AVCodec ff_a64multi_encoder = {
  322. .name = "a64multi",
  323. .type = AVMEDIA_TYPE_VIDEO,
  324. .id = CODEC_ID_A64_MULTI,
  325. .priv_data_size = sizeof(A64Context),
  326. .init = a64multi_init_encoder,
  327. .encode = a64multi_encode_frame,
  328. .close = a64multi_close_encoder,
  329. .pix_fmts = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  330. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
  331. .capabilities = CODEC_CAP_DELAY,
  332. };
  333. AVCodec ff_a64multi5_encoder = {
  334. .name = "a64multi5",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .id = CODEC_ID_A64_MULTI5,
  337. .priv_data_size = sizeof(A64Context),
  338. .init = a64multi_init_encoder,
  339. .encode = a64multi_encode_frame,
  340. .close = a64multi_close_encoder,
  341. .pix_fmts = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  342. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
  343. .capabilities = CODEC_CAP_DELAY,
  344. };