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.

367 lines
12KB

  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. 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. /* set up extradata */
  181. avctx->extradata = av_mallocz(8 * 4 + FF_INPUT_BUFFER_PADDING_SIZE);
  182. avctx->extradata_size = 8 * 4;
  183. AV_WB32(avctx->extradata, c->mc_lifetime);
  184. AV_WB32(avctx->extradata+16, INTERLACED);
  185. avcodec_get_frame_defaults(&c->picture);
  186. avctx->coded_frame = &c->picture;
  187. avctx->coded_frame->pict_type = FF_I_TYPE;
  188. avctx->coded_frame->key_frame = 1;
  189. if (!avctx->codec_tag)
  190. avctx->codec_tag = AV_RL32("a64m");
  191. return 0;
  192. }
  193. static void a64_compress_colram(unsigned char *buf, int *charmap, uint8_t *colram)
  194. {
  195. int a;
  196. uint8_t temp;
  197. /* only needs to be done in 5col mode */
  198. /* XXX could be squeezed to 0x80 bytes */
  199. for (a = 0; a < 256; a++) {
  200. temp = colram[charmap[a + 0x000]] << 0;
  201. temp |= colram[charmap[a + 0x100]] << 1;
  202. temp |= colram[charmap[a + 0x200]] << 2;
  203. if(a < 0xe8) temp |= colram[charmap[a + 0x300]] << 3;
  204. buf[a] = temp << 2;
  205. }
  206. }
  207. static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  208. int buf_size, void *data)
  209. {
  210. A64Context *c = avctx->priv_data;
  211. AVFrame *pict = data;
  212. AVFrame *const p = (AVFrame *) & c->picture;
  213. int frame;
  214. int a;
  215. int req_size;
  216. int num_frames = c->mc_lifetime;
  217. int *charmap = c->mc_charmap;
  218. uint8_t *colram = c->mc_colram;
  219. uint8_t *charset = c->mc_charset;
  220. int *meta = c->mc_meta_charset;
  221. int *best_cb = c->mc_best_cb;
  222. int charset_size = 0x800 * (INTERLACED + 1);
  223. int screen_size = 0x400;
  224. int colram_size = 0x100 * c->mc_use_5col;
  225. /* no data, means end encoding asap */
  226. if (!data) {
  227. /* all done, end encoding */
  228. if(!c->mc_lifetime) return 0;
  229. /* no more frames in queue, prepare to flush remaining frames */
  230. if(!c->mc_frame_counter) {
  231. num_frames=c->mc_lifetime;
  232. c->mc_lifetime=0;
  233. }
  234. /* still frames in queue so limit lifetime to remaining frames */
  235. else c->mc_lifetime=c->mc_frame_counter;
  236. }
  237. /* still new data available */
  238. else {
  239. /* fill up mc_meta_charset with data until lifetime exceeds */
  240. if (c->mc_frame_counter < c->mc_lifetime) {
  241. *p = *pict;
  242. p->pict_type = FF_I_TYPE;
  243. p->key_frame = 1;
  244. to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
  245. c->mc_frame_counter++;
  246. /* lifetime is not reached so wait for next frame first */
  247. return 0;
  248. }
  249. }
  250. /* lifetime reached so now convert X frames at once */
  251. if (c->mc_frame_counter == c->mc_lifetime) {
  252. req_size = 0;
  253. /* any frames to encode? */
  254. if(c->mc_lifetime) {
  255. /* calc optimal new charset + charmaps */
  256. ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  257. ff_do_elbg (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  258. /* create colorram map and a c64 readable charset */
  259. render_charset(avctx, charset, colram);
  260. /* copy charset to buf */
  261. memcpy(buf,charset, charset_size);
  262. /* advance pointers */
  263. buf += charset_size;
  264. charset += charset_size;
  265. req_size += charset_size;
  266. }
  267. /* no charset so clean buf */
  268. else memset(buf, 0, charset_size);
  269. /* write x frames to buf */
  270. for (frame = 0; frame < c->mc_lifetime; frame++) {
  271. /* copy charmap to buf. buf is uchar*, charmap is int*, so no memcpy here, sorry */
  272. for (a = 0; a < 1000; a++) {
  273. buf[a] = charmap[a];
  274. }
  275. /* advance pointers */
  276. buf += screen_size;
  277. req_size += screen_size;
  278. /* compress and copy colram to buf */
  279. if(c->mc_use_5col) {
  280. a64_compress_colram(buf,charmap,colram);
  281. /* advance pointers */
  282. buf += colram_size;
  283. req_size += colram_size;
  284. }
  285. /* advance to next charmap */
  286. charmap += 1000;
  287. }
  288. AV_WB32(avctx->extradata+4, c->mc_frame_counter);
  289. AV_WB32(avctx->extradata+8, charset_size);
  290. AV_WB32(avctx->extradata+12, screen_size + colram_size);
  291. /* reset counter */
  292. c->mc_frame_counter = 0;
  293. if (req_size > buf_size) {
  294. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
  295. return -1;
  296. }
  297. return req_size;
  298. }
  299. return 0;
  300. }
  301. AVCodec a64multi_encoder = {
  302. .name = "a64multi",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .id = CODEC_ID_A64_MULTI,
  305. .priv_data_size = sizeof(A64Context),
  306. .init = a64multi_init_encoder,
  307. .encode = a64multi_encode_frame,
  308. .close = a64multi_close_encoder,
  309. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  310. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
  311. .capabilities = CODEC_CAP_DELAY,
  312. };
  313. AVCodec a64multi5_encoder = {
  314. .name = "a64multi5",
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .id = CODEC_ID_A64_MULTI5,
  317. .priv_data_size = sizeof(A64Context),
  318. .init = a64multi_init_encoder,
  319. .encode = a64multi_encode_frame,
  320. .close = a64multi_close_encoder,
  321. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  322. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
  323. .capabilities = CODEC_CAP_DELAY,
  324. };