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.

249 lines
8.1KB

  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 "a64tables.h"
  27. #include "elbg.h"
  28. #include "libavutil/intreadwrite.h"
  29. #define DITHERSTEPS 8
  30. static void to_meta_with_crop(AVCodecContext *avctx, AVFrame *p, int *dest)
  31. {
  32. int blockx, blocky, x, y;
  33. int luma = 0;
  34. int height = FFMIN(avctx->height,C64YRES);
  35. int width = FFMIN(avctx->width ,C64XRES);
  36. uint8_t *src = p->data[0];
  37. for (blocky = 0; blocky < height; blocky += 8) {
  38. for (blockx = 0; blockx < C64XRES; blockx += 8) {
  39. for (y = blocky; y < blocky+8 && y < height; y++) {
  40. for (x = blockx; x < blockx+8 && x < C64XRES; x += 2) {
  41. if(x < width) {
  42. /* build average over 2 pixels */
  43. luma = (src[(x + 0 + y * p->linesize[0])] +
  44. src[(x + 1 + y * p->linesize[0])]) / 2;
  45. /* write blocks as linear data now so they are suitable for elbg */
  46. dest[0] = luma;
  47. }
  48. dest++;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. static void render_charset(AVCodecContext *avctx, uint8_t *charset,
  55. uint8_t *colrammap)
  56. {
  57. A64Context *c = avctx->priv_data;
  58. uint8_t row1;
  59. int charpos, x, y;
  60. int pix;
  61. int dither;
  62. int index1, index2;
  63. int lowdiff, highdiff;
  64. int maxindex = c->mc_use_5col + 3;
  65. int maxsteps = DITHERSTEPS * maxindex + 1;
  66. int *best_cb = c->mc_best_cb;
  67. /* now reduce colors first */
  68. for (x = 0; x < 256 * 32; x++) best_cb[x] = best_cb[x] * maxsteps / 255;
  69. /* and render charset */
  70. for (charpos = 0; charpos < 256; charpos++) {
  71. lowdiff = 0;
  72. highdiff = 0;
  73. for (y = 0; y < 8; y++) {
  74. row1 = 0;
  75. for (x = 0; x < 4; x++) {
  76. pix = best_cb[y * 4 + x];
  77. dither = pix % DITHERSTEPS;
  78. index1 = pix / DITHERSTEPS;
  79. index2 = FFMIN(index1 + 1, maxindex);
  80. if (pix > 3 * DITHERSTEPS)
  81. highdiff += pix - 3 * DITHERSTEPS;
  82. if (pix < DITHERSTEPS)
  83. lowdiff += DITHERSTEPS - pix;
  84. row1 <<= 2;
  85. if (prep_dither_patterns[dither][y & 3][x & 3]) {
  86. row1 |= 3-(index2 & 3);
  87. } else {
  88. row1 |= 3-(index1 & 3);
  89. }
  90. }
  91. charset[y] = row1;
  92. }
  93. /* are we in 5col mode and need to adjust pixels? */
  94. if (c->mc_use_5col && highdiff > 0 && lowdiff > 0) {
  95. if (lowdiff > highdiff) {
  96. for (x = 0; x < 32; x++)
  97. best_cb[x] = FFMIN(3 * DITHERSTEPS, best_cb[x]);
  98. } else {
  99. for (x = 0; x < 32; x++)
  100. best_cb[x] = FFMAX(DITHERSTEPS, best_cb[x]);
  101. }
  102. charpos--; /* redo char */
  103. } else {
  104. /* advance pointers */
  105. best_cb += 32;
  106. charset += 8;
  107. if (highdiff > 0) {
  108. colrammap[charpos] = 0x9;
  109. } else {
  110. colrammap[charpos] = 0x8;
  111. }
  112. }
  113. }
  114. }
  115. static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
  116. {
  117. A64Context *c = avctx->priv_data;
  118. av_free(c->mc_meta_charset);
  119. av_free(c->mc_best_cb);
  120. av_free(c->mc_charmap);
  121. return 0;
  122. }
  123. static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
  124. {
  125. A64Context *c = avctx->priv_data;
  126. av_lfg_init(&c->randctx, 1);
  127. if (avctx->global_quality < 1) {
  128. c->mc_lifetime = 4;
  129. } else {
  130. c->mc_lifetime = avctx->global_quality /= FF_QP2LAMBDA;
  131. }
  132. av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime);
  133. c->mc_frame_counter = 0;
  134. c->mc_use_5col = avctx->codec->id == CODEC_ID_A64_MULTI5;
  135. c->mc_meta_charset = av_malloc(32000 * c->mc_lifetime * sizeof(int));
  136. c->mc_best_cb = av_malloc(256 * 32 * sizeof(int));
  137. c->mc_charmap = av_malloc(1000 * c->mc_lifetime * sizeof(int));
  138. avcodec_get_frame_defaults(&c->picture);
  139. avctx->coded_frame = &c->picture;
  140. avctx->coded_frame->pict_type = FF_I_TYPE;
  141. avctx->coded_frame->key_frame = 1;
  142. if (!avctx->codec_tag)
  143. avctx->codec_tag = AV_RL32("a64m");
  144. return 0;
  145. }
  146. static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  147. int buf_size, void *data)
  148. {
  149. A64Context *c = avctx->priv_data;
  150. AVFrame *pict = data;
  151. AVFrame *const p = (AVFrame *) & c->picture;
  152. int frame;
  153. int a;
  154. uint8_t colrammap[256];
  155. int *charmap = c->mc_charmap;
  156. int *meta = c->mc_meta_charset;
  157. int *best_cb = c->mc_best_cb;
  158. int frm_size = 0x400 + 0x400 * c->mc_use_5col;
  159. int req_size;
  160. /* it is the last frame so prepare to flush */
  161. if (!data)
  162. c->mc_lifetime = c->mc_frame_counter;
  163. req_size = 0x800 + frm_size * c->mc_lifetime;
  164. if (req_size > buf_size) {
  165. av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
  166. return -1;
  167. }
  168. /* fill up mc_meta_charset with framedata until lifetime exceeds */
  169. if (c->mc_frame_counter < c->mc_lifetime) {
  170. *p = *pict;
  171. p->pict_type = FF_I_TYPE;
  172. p->key_frame = 1;
  173. to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
  174. c->mc_frame_counter++;
  175. /* lifetime is not reached */
  176. return 0;
  177. }
  178. /* lifetime exceeded so now convert X frames at once */
  179. if (c->mc_frame_counter == c->mc_lifetime && c->mc_lifetime > 0) {
  180. c->mc_frame_counter = 0;
  181. ff_init_elbg(meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx);
  182. ff_do_elbg (meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx);
  183. render_charset(avctx, buf, colrammap);
  184. for (frame = 0; frame < c->mc_lifetime; frame++) {
  185. for (a = 0; a < 1000; a++) {
  186. buf[0x800 + a] = charmap[a];
  187. if (c->mc_use_5col) buf[0xc00 + a] = colrammap[charmap[a]];
  188. }
  189. buf += frm_size;
  190. charmap += 1000;
  191. }
  192. return req_size;
  193. }
  194. return 0;
  195. }
  196. AVCodec a64multi_encoder = {
  197. .name = "a64multi",
  198. .type = CODEC_TYPE_VIDEO,
  199. .id = CODEC_ID_A64_MULTI,
  200. .priv_data_size = sizeof(A64Context),
  201. .init = a64multi_init_encoder,
  202. .encode = a64multi_encode_frame,
  203. .close = a64multi_close_encoder,
  204. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  205. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
  206. .capabilities = CODEC_CAP_DELAY,
  207. };
  208. AVCodec a64multi5_encoder = {
  209. .name = "a64multi5",
  210. .type = CODEC_TYPE_VIDEO,
  211. .id = CODEC_ID_A64_MULTI5,
  212. .priv_data_size = sizeof(A64Context),
  213. .init = a64multi_init_encoder,
  214. .encode = a64multi_encode_frame,
  215. .close = a64multi_close_encoder,
  216. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
  217. .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
  218. .capabilities = CODEC_CAP_DELAY,
  219. };