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.

431 lines
14KB

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