00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "a64enc.h"
00028 #include "a64colors.h"
00029 #include "a64tables.h"
00030 #include "elbg.h"
00031 #include "internal.h"
00032 #include "libavutil/common.h"
00033 #include "libavutil/intreadwrite.h"
00034
00035 #define DITHERSTEPS 8
00036 #define CHARSET_CHARS 256
00037 #define INTERLACED 1
00038 #define CROP_SCREENS 1
00039
00040
00041 static const int mc_colors[5]={0x0,0xb,0xc,0xf,0x1};
00042
00043
00044
00045
00046
00047 static void to_meta_with_crop(AVCodecContext *avctx, AVFrame *p, int *dest)
00048 {
00049 int blockx, blocky, x, y;
00050 int luma = 0;
00051 int height = FFMIN(avctx->height, C64YRES);
00052 int width = FFMIN(avctx->width , C64XRES);
00053 uint8_t *src = p->data[0];
00054
00055 for (blocky = 0; blocky < C64YRES; blocky += 8) {
00056 for (blockx = 0; blockx < C64XRES; blockx += 8) {
00057 for (y = blocky; y < blocky + 8 && y < C64YRES; y++) {
00058 for (x = blockx; x < blockx + 8 && x < C64XRES; x += 2) {
00059 if(x < width && y < height) {
00060
00061 luma = (src[(x + 0 + y * p->linesize[0])] +
00062 src[(x + 1 + y * p->linesize[0])]) / 2;
00063
00064 dest[0] = luma;
00065 }
00066 dest++;
00067 }
00068 }
00069 }
00070 }
00071 }
00072
00073 static void render_charset(AVCodecContext *avctx, uint8_t *charset,
00074 uint8_t *colrammap)
00075 {
00076 A64Context *c = avctx->priv_data;
00077 uint8_t row1, row2;
00078 int charpos, x, y;
00079 int a, b;
00080 uint8_t pix;
00081 int lowdiff, highdiff;
00082 int *best_cb = c->mc_best_cb;
00083 static uint8_t index1[256];
00084 static uint8_t index2[256];
00085 static uint8_t dither[256];
00086 int i;
00087 int distance;
00088
00089
00090 i = 0;
00091 for (a=0; a < 256; a++) {
00092 if(i < c->mc_pal_size -1 && a == c->mc_luma_vals[i + 1]) {
00093 distance = c->mc_luma_vals[i + 1] - c->mc_luma_vals[i];
00094 for(b = 0; b <= distance; b++) {
00095 dither[c->mc_luma_vals[i] + b] = b * (DITHERSTEPS - 1) / distance;
00096 }
00097 i++;
00098 }
00099 if(i >= c->mc_pal_size - 1) dither[a] = 0;
00100 index1[a] = i;
00101 index2[a] = FFMIN(i + 1, c->mc_pal_size - 1);
00102 }
00103
00104
00105 for (charpos = 0; charpos < CHARSET_CHARS; charpos++) {
00106 lowdiff = 0;
00107 highdiff = 0;
00108 for (y = 0; y < 8; y++) {
00109 row1 = 0; row2 = 0;
00110 for (x = 0; x < 4; x++) {
00111 pix = best_cb[y * 4 + x];
00112
00113
00114 if (index1[pix] >= 3)
00115 highdiff += pix - c->mc_luma_vals[3];
00116 if (index1[pix] < 1)
00117 lowdiff += c->mc_luma_vals[1] - pix;
00118
00119 row1 <<= 2;
00120
00121 if (INTERLACED) {
00122 row2 <<= 2;
00123 if (interlaced_dither_patterns[dither[pix]][(y & 3) * 2 + 0][x & 3])
00124 row1 |= 3-(index2[pix] & 3);
00125 else
00126 row1 |= 3-(index1[pix] & 3);
00127
00128 if (interlaced_dither_patterns[dither[pix]][(y & 3) * 2 + 1][x & 3])
00129 row2 |= 3-(index2[pix] & 3);
00130 else
00131 row2 |= 3-(index1[pix] & 3);
00132 }
00133 else {
00134 if (multi_dither_patterns[dither[pix]][(y & 3)][x & 3])
00135 row1 |= 3-(index2[pix] & 3);
00136 else
00137 row1 |= 3-(index1[pix] & 3);
00138 }
00139 }
00140 charset[y+0x000] = row1;
00141 if (INTERLACED) charset[y+0x800] = row2;
00142 }
00143
00144 if (highdiff > 0 && lowdiff > 0 && c->mc_use_5col) {
00145 if (lowdiff > highdiff) {
00146 for (x = 0; x < 32; x++)
00147 best_cb[x] = FFMIN(c->mc_luma_vals[3], best_cb[x]);
00148 } else {
00149 for (x = 0; x < 32; x++)
00150 best_cb[x] = FFMAX(c->mc_luma_vals[1], best_cb[x]);
00151 }
00152 charpos--;
00153
00154 } else {
00155
00156 best_cb += 32;
00157 charset += 8;
00158
00159
00160 colrammap[charpos] = (highdiff > 0);
00161 }
00162 }
00163 }
00164
00165 static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
00166 {
00167 A64Context *c = avctx->priv_data;
00168 av_free(c->mc_meta_charset);
00169 av_free(c->mc_best_cb);
00170 av_free(c->mc_charset);
00171 av_free(c->mc_charmap);
00172 av_free(c->mc_colram);
00173 return 0;
00174 }
00175
00176 static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
00177 {
00178 A64Context *c = avctx->priv_data;
00179 int a;
00180 av_lfg_init(&c->randctx, 1);
00181
00182 if (avctx->global_quality < 1) {
00183 c->mc_lifetime = 4;
00184 } else {
00185 c->mc_lifetime = avctx->global_quality /= FF_QP2LAMBDA;
00186 }
00187
00188 av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime);
00189
00190 c->mc_frame_counter = 0;
00191 c->mc_use_5col = avctx->codec->id == AV_CODEC_ID_A64_MULTI5;
00192 c->mc_pal_size = 4 + c->mc_use_5col;
00193
00194
00195 for (a = 0; a < c->mc_pal_size; a++) {
00196 c->mc_luma_vals[a]=a64_palette[mc_colors[a]][0] * 0.30 +
00197 a64_palette[mc_colors[a]][1] * 0.59 +
00198 a64_palette[mc_colors[a]][2] * 0.11;
00199 }
00200
00201 if (!(c->mc_meta_charset = av_malloc(32000 * c->mc_lifetime * sizeof(int))) ||
00202 !(c->mc_best_cb = av_malloc(CHARSET_CHARS * 32 * sizeof(int))) ||
00203 !(c->mc_charmap = av_mallocz(1000 * c->mc_lifetime * sizeof(int))) ||
00204 !(c->mc_colram = av_mallocz(CHARSET_CHARS * sizeof(uint8_t))) ||
00205 !(c->mc_charset = av_malloc(0x800 * (INTERLACED+1) * sizeof(uint8_t)))) {
00206 av_log(avctx, AV_LOG_ERROR, "Failed to allocate buffer memory.\n");
00207 return AVERROR(ENOMEM);
00208 }
00209
00210
00211 if (!(avctx->extradata = av_mallocz(8 * 4 + FF_INPUT_BUFFER_PADDING_SIZE))) {
00212 av_log(avctx, AV_LOG_ERROR, "Failed to allocate memory for extradata.\n");
00213 return AVERROR(ENOMEM);
00214 }
00215 avctx->extradata_size = 8 * 4;
00216 AV_WB32(avctx->extradata, c->mc_lifetime);
00217 AV_WB32(avctx->extradata + 16, INTERLACED);
00218
00219 avcodec_get_frame_defaults(&c->picture);
00220 avctx->coded_frame = &c->picture;
00221 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00222 avctx->coded_frame->key_frame = 1;
00223 if (!avctx->codec_tag)
00224 avctx->codec_tag = AV_RL32("a64m");
00225
00226 c->next_pts = AV_NOPTS_VALUE;
00227
00228 return 0;
00229 }
00230
00231 static void a64_compress_colram(unsigned char *buf, int *charmap, uint8_t *colram)
00232 {
00233 int a;
00234 uint8_t temp;
00235
00236
00237 for (a = 0; a < 256; a++) {
00238 temp = colram[charmap[a + 0x000]] << 0;
00239 temp |= colram[charmap[a + 0x100]] << 1;
00240 temp |= colram[charmap[a + 0x200]] << 2;
00241 if (a < 0xe8) temp |= colram[charmap[a + 0x300]] << 3;
00242 buf[a] = temp << 2;
00243 }
00244 }
00245
00246 static int a64multi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00247 const AVFrame *pict, int *got_packet)
00248 {
00249 A64Context *c = avctx->priv_data;
00250 AVFrame *const p = &c->picture;
00251
00252 int frame;
00253 int x, y;
00254 int b_height;
00255 int b_width;
00256
00257 int req_size, ret;
00258 uint8_t *buf = NULL;
00259
00260 int *charmap = c->mc_charmap;
00261 uint8_t *colram = c->mc_colram;
00262 uint8_t *charset = c->mc_charset;
00263 int *meta = c->mc_meta_charset;
00264 int *best_cb = c->mc_best_cb;
00265
00266 int charset_size = 0x800 * (INTERLACED + 1);
00267 int colram_size = 0x100 * c->mc_use_5col;
00268 int screen_size;
00269
00270 if(CROP_SCREENS) {
00271 b_height = FFMIN(avctx->height,C64YRES) >> 3;
00272 b_width = FFMIN(avctx->width ,C64XRES) >> 3;
00273 screen_size = b_width * b_height;
00274 } else {
00275 b_height = C64YRES >> 3;
00276 b_width = C64XRES >> 3;
00277 screen_size = 0x400;
00278 }
00279
00280
00281 if (!pict) {
00282
00283 if (!c->mc_lifetime) return 0;
00284
00285 if (!c->mc_frame_counter) {
00286 c->mc_lifetime = 0;
00287 }
00288
00289 else c->mc_lifetime = c->mc_frame_counter;
00290
00291 } else {
00292
00293 if (c->mc_frame_counter < c->mc_lifetime) {
00294 *p = *pict;
00295 p->pict_type = AV_PICTURE_TYPE_I;
00296 p->key_frame = 1;
00297 to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
00298 c->mc_frame_counter++;
00299 if (c->next_pts == AV_NOPTS_VALUE)
00300 c->next_pts = pict->pts;
00301
00302 return 0;
00303 }
00304 }
00305
00306
00307 if (c->mc_frame_counter == c->mc_lifetime) {
00308 req_size = 0;
00309
00310 if (c->mc_lifetime) {
00311 req_size = charset_size + c->mc_lifetime*(screen_size + colram_size);
00312 if ((ret = ff_alloc_packet2(avctx, pkt, req_size)) < 0)
00313 return ret;
00314 buf = pkt->data;
00315
00316
00317 ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
00318 ff_do_elbg (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
00319
00320
00321 render_charset(avctx, charset, colram);
00322
00323
00324 memcpy(buf, charset, charset_size);
00325
00326
00327 buf += charset_size;
00328 charset += charset_size;
00329 }
00330
00331
00332 for (frame = 0; frame < c->mc_lifetime; frame++) {
00333
00334 for (y = 0; y < b_height; y++) {
00335 for (x = 0; x < b_width; x++) {
00336 buf[y * b_width + x] = charmap[y * b_width + x];
00337 }
00338 }
00339
00340 buf += screen_size;
00341 req_size += screen_size;
00342
00343
00344 if (c->mc_use_5col) {
00345 a64_compress_colram(buf, charmap, colram);
00346
00347 buf += colram_size;
00348 req_size += colram_size;
00349 }
00350
00351
00352 charmap += 1000;
00353 }
00354
00355 AV_WB32(avctx->extradata + 4, c->mc_frame_counter);
00356 AV_WB32(avctx->extradata + 8, charset_size);
00357 AV_WB32(avctx->extradata + 12, screen_size + colram_size);
00358
00359
00360 c->mc_frame_counter = 0;
00361
00362 pkt->pts = pkt->dts = c->next_pts;
00363 c->next_pts = AV_NOPTS_VALUE;
00364
00365 pkt->size = req_size;
00366 pkt->flags |= AV_PKT_FLAG_KEY;
00367 *got_packet = !!req_size;
00368 }
00369 return 0;
00370 }
00371
00372 #if CONFIG_A64MULTI_ENCODER
00373 AVCodec ff_a64multi_encoder = {
00374 .name = "a64multi",
00375 .type = AVMEDIA_TYPE_VIDEO,
00376 .id = AV_CODEC_ID_A64_MULTI,
00377 .priv_data_size = sizeof(A64Context),
00378 .init = a64multi_init_encoder,
00379 .encode2 = a64multi_encode_frame,
00380 .close = a64multi_close_encoder,
00381 .pix_fmts = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
00382 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
00383 .capabilities = CODEC_CAP_DELAY,
00384 };
00385 #endif
00386 #if CONFIG_A64MULTI5_ENCODER
00387 AVCodec ff_a64multi5_encoder = {
00388 .name = "a64multi5",
00389 .type = AVMEDIA_TYPE_VIDEO,
00390 .id = AV_CODEC_ID_A64_MULTI5,
00391 .priv_data_size = sizeof(A64Context),
00392 .init = a64multi_init_encoder,
00393 .encode2 = a64multi_encode_frame,
00394 .close = a64multi_close_encoder,
00395 .pix_fmts = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
00396 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
00397 .capabilities = CODEC_CAP_DELAY,
00398 };
00399 #endif