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.

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