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.

388 lines
13KB

  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 *charmap = c->mc_charmap;
  227. uint8_t *colram = c->mc_colram;
  228. uint8_t *charset = c->mc_charset;
  229. int *meta = c->mc_meta_charset;
  230. int *best_cb = c->mc_best_cb;
  231. int charset_size = 0x800 * (INTERLACED + 1);
  232. int colram_size = 0x100 * c->mc_use_5col;
  233. int screen_size;
  234. if(CROP_SCREENS) {
  235. b_height = FFMIN(avctx->height,C64YRES) >> 3;
  236. b_width = FFMIN(avctx->width ,C64XRES) >> 3;
  237. screen_size = b_width * b_height;
  238. } else {
  239. b_height = C64YRES >> 3;
  240. b_width = C64XRES >> 3;
  241. screen_size = 0x400;
  242. }
  243. /* no data, means end encoding asap */
  244. if (!data) {
  245. /* all done, end encoding */
  246. if (!c->mc_lifetime) return 0;
  247. /* no more frames in queue, prepare to flush remaining frames */
  248. if (!c->mc_frame_counter) {
  249. c->mc_lifetime = 0;
  250. }
  251. /* still frames in queue so limit lifetime to remaining frames */
  252. else c->mc_lifetime = c->mc_frame_counter;
  253. /* still new data available */
  254. } else {
  255. /* fill up mc_meta_charset with data until lifetime exceeds */
  256. if (c->mc_frame_counter < c->mc_lifetime) {
  257. *p = *pict;
  258. p->pict_type = AV_PICTURE_TYPE_I;
  259. p->key_frame = 1;
  260. to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
  261. c->mc_frame_counter++;
  262. /* lifetime is not reached so wait for next frame first */
  263. return 0;
  264. }
  265. }
  266. /* lifetime reached so now convert X frames at once */
  267. if (c->mc_frame_counter == c->mc_lifetime) {
  268. req_size = 0;
  269. /* any frames to encode? */
  270. if (c->mc_lifetime) {
  271. /* calc optimal new charset + charmaps */
  272. ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  273. ff_do_elbg (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  274. /* create colorram map and a c64 readable charset */
  275. render_charset(avctx, charset, colram);
  276. /* copy charset to buf */
  277. memcpy(buf,charset, charset_size);
  278. /* advance pointers */
  279. buf += charset_size;
  280. charset += charset_size;
  281. req_size += charset_size;
  282. }
  283. /* no charset so clean buf */
  284. else memset(buf, 0, charset_size);
  285. /* write x frames to buf */
  286. for (frame = 0; frame < c->mc_lifetime; frame++) {
  287. /* copy charmap to buf. buf is uchar*, charmap is int*, so no memcpy here, sorry */
  288. for (y = 0; y < b_height; y++) {
  289. for (x = 0; x < b_width; x++) {
  290. buf[y * b_width + x] = charmap[y * b_width + x];
  291. }
  292. }
  293. /* advance pointers */
  294. buf += screen_size;
  295. req_size += screen_size;
  296. /* compress and copy colram to buf */
  297. if (c->mc_use_5col) {
  298. a64_compress_colram(buf, charmap, colram);
  299. /* advance pointers */
  300. buf += colram_size;
  301. req_size += colram_size;
  302. }
  303. /* advance to next charmap */
  304. charmap += 1000;
  305. }
  306. AV_WB32(avctx->extradata + 4, c->mc_frame_counter);
  307. AV_WB32(avctx->extradata + 8, charset_size);
  308. AV_WB32(avctx->extradata + 12, screen_size + colram_size);
  309. /* reset counter */
  310. c->mc_frame_counter = 0;
  311. if (req_size > buf_size) {
  312. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
  313. return -1;
  314. }
  315. return req_size;
  316. }
  317. return 0;
  318. }
  319. AVCodec ff_a64multi_encoder = {
  320. .name = "a64multi",
  321. .type = AVMEDIA_TYPE_VIDEO,
  322. .id = CODEC_ID_A64_MULTI,
  323. .priv_data_size = sizeof(A64Context),
  324. .init = a64multi_init_encoder,
  325. .encode = a64multi_encode_frame,
  326. .close = a64multi_close_encoder,
  327. .pix_fmts = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  328. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
  329. .capabilities = CODEC_CAP_DELAY,
  330. };
  331. AVCodec ff_a64multi5_encoder = {
  332. .name = "a64multi5",
  333. .type = AVMEDIA_TYPE_VIDEO,
  334. .id = CODEC_ID_A64_MULTI5,
  335. .priv_data_size = sizeof(A64Context),
  336. .init = a64multi_init_encoder,
  337. .encode = a64multi_encode_frame,
  338. .close = a64multi_close_encoder,
  339. .pix_fmts = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  340. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
  341. .capabilities = CODEC_CAP_DELAY,
  342. };