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.

353 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. #define LIFETIME 4
  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 < height; blocky += 8) {
  47. for (blockx = 0; blockx < C64XRES; blockx += 8) {
  48. for (y = blocky; y < blocky+8 && y < height; y++) {
  49. for (x = blockx; x < blockx+8 && x < C64XRES; x += 2) {
  50. if(x < width) {
  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 < 4 && 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 >=4 ) dither[a] = 0;
  89. index1[a] = i;
  90. index2[a] = FFMIN(i+1, 4);
  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) {
  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. /* precalc luma values for later use */
  169. for (a = 0; a < 5; a++) {
  170. c->mc_luma_vals[a]=a64_palette[mc_colors[a]][0] * 0.30 +
  171. a64_palette[mc_colors[a]][1] * 0.59 +
  172. a64_palette[mc_colors[a]][2] * 0.11;
  173. }
  174. c->mc_frame_counter = 0;
  175. c->mc_use_5col = avctx->codec->id == CODEC_ID_A64_MULTI5;
  176. c->mc_meta_charset = av_malloc (32000 * c->mc_lifetime * sizeof(int));
  177. c->mc_best_cb = av_malloc (CHARSET_CHARS * 32 * sizeof(int));
  178. c->mc_charmap = av_mallocz(1000 * c->mc_lifetime * sizeof(int));
  179. c->mc_colram = av_mallocz(CHARSET_CHARS * sizeof(uint8_t));
  180. c->mc_charset = av_malloc (0x800 * (INTERLACED+1) * sizeof(uint8_t));
  181. avcodec_get_frame_defaults(&c->picture);
  182. avctx->coded_frame = &c->picture;
  183. avctx->coded_frame->pict_type = FF_I_TYPE;
  184. avctx->coded_frame->key_frame = 1;
  185. if (!avctx->codec_tag)
  186. avctx->codec_tag = AV_RL32("a64m");
  187. return 0;
  188. }
  189. static void a64_compress_colram(unsigned char *buf, int *charmap, uint8_t *colram)
  190. {
  191. int a;
  192. uint8_t temp;
  193. /* only needs to be done in 5col mode */
  194. /* XXX could be squeezed to 0x80 bytes */
  195. for (a = 0; a < 256; a++) {
  196. temp = colram[charmap[a + 0x000]] << 0;
  197. temp |= colram[charmap[a + 0x100]] << 1;
  198. temp |= colram[charmap[a + 0x200]] << 2;
  199. if(a < 0xe8) temp |= colram[charmap[a + 0x300]] << 3;
  200. buf[a] = temp << 2;
  201. }
  202. }
  203. static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  204. int buf_size, void *data)
  205. {
  206. A64Context *c = avctx->priv_data;
  207. AVFrame *pict = data;
  208. AVFrame *const p = (AVFrame *) & c->picture;
  209. int frame;
  210. int a;
  211. int req_size;
  212. int num_frames = LIFETIME;
  213. int *charmap = c->mc_charmap;
  214. uint8_t *colram = c->mc_colram;
  215. uint8_t *charset = c->mc_charset;
  216. int *meta = c->mc_meta_charset;
  217. int *best_cb = c->mc_best_cb;
  218. /* no data, means end encoding asap */
  219. if (!data) {
  220. /* all done, end encoding */
  221. if(!c->mc_lifetime) return 0;
  222. /* no more frames in queue, prepare to flush remaining frames */
  223. if(!c->mc_frame_counter) {
  224. num_frames=c->mc_lifetime;
  225. c->mc_lifetime=0;
  226. }
  227. /* still frames in queue so limit lifetime to remaining frames */
  228. else c->mc_lifetime=c->mc_frame_counter;
  229. }
  230. /* still new data available */
  231. else {
  232. /* fill up mc_meta_charset with data until lifetime exceeds */
  233. if (c->mc_frame_counter < c->mc_lifetime) {
  234. *p = *pict;
  235. p->pict_type = FF_I_TYPE;
  236. p->key_frame = 1;
  237. to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
  238. c->mc_frame_counter++;
  239. /* lifetime is not reached so wait for next frame first */
  240. return 0;
  241. }
  242. }
  243. /* lifetime reached so now convert X frames at once */
  244. if (c->mc_frame_counter == c->mc_lifetime) {
  245. /* any frames to encode? */
  246. if(c->mc_lifetime) {
  247. /* calc optimal new charset + charmaps */
  248. ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  249. ff_do_elbg (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
  250. /* create colorram map and a c64 readable charset */
  251. render_charset(avctx, charset, colram);
  252. }
  253. req_size = 0;
  254. //XXX this all should maybe move to the muxer? as well as teh chunked/not chunked thing?
  255. /* either write charset as a whole (more comfy when playing from mem) */
  256. /* copy charset chunk if exists */
  257. if(c->mc_lifetime) {
  258. memcpy(buf,charset,0x800*(INTERLACED+1));
  259. buf += 0x800*(INTERLACED+1);
  260. charset += 0x800*(INTERLACED+1);
  261. req_size += 0x800*(INTERLACED+1);
  262. }
  263. else memset(buf,0,0x800*(INTERLACED+1));
  264. /* write x frames to buf */
  265. for (frame = 0; frame < c->mc_lifetime; frame++) {
  266. /* buf is uchar*, charmap is int*, so no memcpy here, sorry */
  267. for (a = 0; a < 1000; a++) {
  268. buf[a] = charmap[a];
  269. }
  270. buf += 0x400;
  271. req_size += 0x400;
  272. if(c->mc_use_5col) {
  273. a64_compress_colram(buf,charmap,colram);
  274. buf += 0x100;
  275. req_size += 0x100;
  276. }
  277. /* advance to next charmap */
  278. charmap += 1000;
  279. }
  280. /* reset counter */
  281. c->mc_frame_counter = 0;
  282. if (req_size > buf_size) {
  283. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
  284. return -1;
  285. }
  286. return req_size;
  287. }
  288. return 0;
  289. }
  290. AVCodec a64multi_encoder = {
  291. .name = "a64multi",
  292. .type = AVMEDIA_TYPE_VIDEO,
  293. .id = CODEC_ID_A64_MULTI,
  294. .priv_data_size = sizeof(A64Context),
  295. .init = a64multi_init_encoder,
  296. .encode = a64multi_encode_frame,
  297. .close = a64multi_close_encoder,
  298. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  299. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
  300. .capabilities = CODEC_CAP_DELAY,
  301. };
  302. AVCodec a64multi5_encoder = {
  303. .name = "a64multi5",
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .id = CODEC_ID_A64_MULTI5,
  306. .priv_data_size = sizeof(A64Context),
  307. .init = a64multi_init_encoder,
  308. .encode = a64multi_encode_frame,
  309. .close = a64multi_close_encoder,
  310. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  311. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
  312. .capabilities = CODEC_CAP_DELAY,
  313. };