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