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.

398 lines
13KB

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