2 * a64 video encoder - multicolor modes
3 * Copyright (c) 2009 Tobias Bindhammer
5 * This file is part of FFmpeg.
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.
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.
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
24 * a64 video encoder - multicolor modes
28 #include "a64tables.h"
30 #include "libavutil/intreadwrite.h"
34 static void to_meta_with_crop(AVCodecContext *avctx, AVFrame *p, int *dest)
36 int blockx, blocky, x, y;
38 int height = FFMIN(avctx->height,C64YRES);
39 int width = FFMIN(avctx->width ,C64XRES);
40 uint8_t *src = p->data[0];
42 for (blocky = 0; blocky < height; blocky += 8) {
43 for (blockx = 0; blockx < C64XRES; blockx += 8) {
44 for (y = blocky; y < blocky+8 && y < height; y++) {
45 for (x = blockx; x < blockx+8 && x < C64XRES; x += 2) {
47 /* build average over 2 pixels */
48 luma = (src[(x + 0 + y * p->linesize[0])] +
49 src[(x + 1 + y * p->linesize[0])]) / 2;
50 /* write blocks as linear data now so they are suitable for elbg */
60 static void render_charset(AVCodecContext *avctx, uint8_t *charset,
63 A64Context *c = avctx->priv_data;
69 int lowdiff, highdiff;
70 int maxindex = c->mc_use_5col + 3;
71 int maxsteps = DITHERSTEPS * maxindex + 1;
72 int *best_cb = c->mc_best_cb;
74 /* now reduce colors first */
75 for (x = 0; x < 256 * 32; x++) best_cb[x] = best_cb[x] * maxsteps / 255;
77 /* and render charset */
78 for (charpos = 0; charpos < 256; charpos++) {
81 for (y = 0; y < 8; y++) {
83 for (x = 0; x < 4; x++) {
84 pix = best_cb[y * 4 + x];
85 dither = pix % DITHERSTEPS;
86 index1 = pix / DITHERSTEPS;
87 index2 = FFMIN(index1 + 1, maxindex);
89 if (pix > 3 * DITHERSTEPS)
90 highdiff += pix - 3 * DITHERSTEPS;
91 if (pix < DITHERSTEPS)
92 lowdiff += DITHERSTEPS - pix;
95 if (prep_dither_patterns[dither][y & 3][x & 3]) {
96 row1 |= 3-(index2 & 3);
98 row1 |= 3-(index1 & 3);
104 /* are we in 5col mode and need to adjust pixels? */
105 if (c->mc_use_5col && highdiff > 0 && lowdiff > 0) {
106 if (lowdiff > highdiff) {
107 for (x = 0; x < 32; x++)
108 best_cb[x] = FFMIN(3 * DITHERSTEPS, best_cb[x]);
110 for (x = 0; x < 32; x++)
111 best_cb[x] = FFMAX(DITHERSTEPS, best_cb[x]);
113 charpos--; /* redo char */
115 /* advance pointers */
120 colrammap[charpos] = 0x9;
122 colrammap[charpos] = 0x8;
128 static av_cold int a64multi_close_encoder(AVCodecContext *avctx)
130 A64Context *c = avctx->priv_data;
131 av_free(c->mc_meta_charset);
132 av_free(c->mc_best_cb);
133 av_free(c->mc_charmap);
137 static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
139 A64Context *c = avctx->priv_data;
140 av_lfg_init(&c->randctx, 1);
142 if (avctx->global_quality < 1) {
145 c->mc_lifetime = avctx->global_quality /= FF_QP2LAMBDA;
148 av_log(avctx, AV_LOG_INFO, "charset lifetime set to %d frame(s)\n", c->mc_lifetime);
150 c->mc_frame_counter = 0;
151 c->mc_use_5col = avctx->codec->id == CODEC_ID_A64_MULTI5;
152 c->mc_meta_charset = av_malloc(32000 * c->mc_lifetime * sizeof(int));
153 c->mc_best_cb = av_malloc(256 * 32 * sizeof(int));
154 c->mc_charmap = av_malloc(1000 * c->mc_lifetime * sizeof(int));
156 avcodec_get_frame_defaults(&c->picture);
157 avctx->coded_frame = &c->picture;
158 avctx->coded_frame->pict_type = FF_I_TYPE;
159 avctx->coded_frame->key_frame = 1;
160 if (!avctx->codec_tag)
161 avctx->codec_tag = AV_RL32("a64m");
166 static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
167 int buf_size, void *data)
169 A64Context *c = avctx->priv_data;
170 AVFrame *pict = data;
171 AVFrame *const p = (AVFrame *) & c->picture;
176 uint8_t colrammap[256];
177 int *charmap = c->mc_charmap;
178 int *meta = c->mc_meta_charset;
179 int *best_cb = c->mc_best_cb;
180 int frm_size = 0x400 + 0x400 * c->mc_use_5col;
183 /* it is the last frame so prepare to flush */
185 c->mc_lifetime = c->mc_frame_counter;
187 req_size = 0x800 + frm_size * c->mc_lifetime;
189 if (req_size > buf_size) {
190 av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
193 /* fill up mc_meta_charset with framedata until lifetime exceeds */
194 if (c->mc_frame_counter < c->mc_lifetime) {
196 p->pict_type = FF_I_TYPE;
198 to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
199 c->mc_frame_counter++;
200 /* lifetime is not reached */
203 /* lifetime exceeded so now convert X frames at once */
204 if (c->mc_frame_counter == c->mc_lifetime && c->mc_lifetime > 0) {
205 c->mc_frame_counter = 0;
206 ff_init_elbg(meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx);
207 ff_do_elbg (meta, 32, 1000 * c-> mc_lifetime, best_cb, 256, 5, charmap, &c->randctx);
209 render_charset(avctx, buf, colrammap);
211 for (frame = 0; frame < c->mc_lifetime; frame++) {
212 for (a = 0; a < 1000; a++) {
213 buf[0x800 + a] = charmap[a];
214 if (c->mc_use_5col) buf[0xc00 + a] = colrammap[charmap[a]];
224 AVCodec a64multi_encoder = {
226 .type = CODEC_TYPE_VIDEO,
227 .id = CODEC_ID_A64_MULTI,
228 .priv_data_size = sizeof(A64Context),
229 .init = a64multi_init_encoder,
230 .encode = a64multi_encode_frame,
231 .close = a64multi_close_encoder,
232 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
233 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
234 .capabilities = CODEC_CAP_DELAY,
237 AVCodec a64multi5_encoder = {
239 .type = CODEC_TYPE_VIDEO,
240 .id = CODEC_ID_A64_MULTI5,
241 .priv_data_size = sizeof(A64Context),
242 .init = a64multi_init_encoder,
243 .encode = a64multi_encode_frame,
244 .close = a64multi_close_encoder,
245 .pix_fmts = (enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
246 .long_name = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
247 .capabilities = CODEC_CAP_DELAY,