00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/common.h"
00028 #include "libavutil/lfg.h"
00029 #include "avcodec.h"
00030 #include "cga_data.h"
00031
00032 #define ATTR_BOLD 0x01
00033 #define ATTR_FAINT 0x02
00034 #define ATTR_UNDERLINE 0x08
00035 #define ATTR_BLINK 0x10
00036 #define ATTR_REVERSE 0x40
00037 #define ATTR_CONCEALED 0x80
00039 #define DEFAULT_FG_COLOR 7
00040 #define DEFAULT_BG_COLOR 0
00041 #define DEFAULT_SCREEN_MODE 3
00043 #define FONT_WIDTH 8
00046 static const uint8_t ansi_to_cga[16] = {
00047 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
00048 };
00049
00050 typedef struct {
00051 AVFrame frame;
00052 int x;
00053 int y;
00054 int sx;
00055 int sy;
00056 const uint8_t* font;
00057 int font_height;
00058 int attributes;
00059 int fg;
00060 int bg;
00061 int first_frame;
00062
00063
00064 enum {
00065 STATE_NORMAL = 0,
00066 STATE_ESCAPE,
00067 STATE_CODE,
00068 STATE_MUSIC_PREAMBLE
00069 } state;
00070 #define MAX_NB_ARGS 4
00071 int args[MAX_NB_ARGS];
00072 int nb_args;
00073 } AnsiContext;
00074
00075 static av_cold int decode_init(AVCodecContext *avctx)
00076 {
00077 AnsiContext *s = avctx->priv_data;
00078 avctx->pix_fmt = PIX_FMT_PAL8;
00079
00080
00081 s->font = ff_vga16_font;
00082 s->font_height = 16;
00083 s->fg = DEFAULT_FG_COLOR;
00084 s->bg = DEFAULT_BG_COLOR;
00085
00086 avcodec_get_frame_defaults(&s->frame);
00087 if (!avctx->width || !avctx->height)
00088 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
00089
00090 return 0;
00091 }
00092
00093 static void set_palette(uint32_t *pal)
00094 {
00095 int r, g, b;
00096 memcpy(pal, ff_cga_palette, 16 * 4);
00097 pal += 16;
00098 #define COLOR(x) ((x) * 40 + 55)
00099 for (r = 0; r < 6; r++)
00100 for (g = 0; g < 6; g++)
00101 for (b = 0; b < 6; b++)
00102 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
00103 #define GRAY(x) ((x) * 10 + 8)
00104 for (g = 0; g < 24; g++)
00105 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
00106 }
00107
00108 static void hscroll(AVCodecContext *avctx)
00109 {
00110 AnsiContext *s = avctx->priv_data;
00111 int i;
00112
00113 if (s->y < avctx->height - s->font_height) {
00114 s->y += s->font_height;
00115 return;
00116 }
00117
00118 i = 0;
00119 for (; i < avctx->height - s->font_height; i++)
00120 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
00121 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
00122 avctx->width);
00123 for (; i < avctx->height; i++)
00124 memset(s->frame.data[0] + i * s->frame.linesize[0],
00125 DEFAULT_BG_COLOR, avctx->width);
00126 }
00127
00128 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
00129 {
00130 AnsiContext *s = avctx->priv_data;
00131 int i;
00132 for (i = 0; i < s->font_height; i++)
00133 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
00134 DEFAULT_BG_COLOR, xlength);
00135 }
00136
00137 static void erase_screen(AVCodecContext *avctx)
00138 {
00139 AnsiContext *s = avctx->priv_data;
00140 int i;
00141 for (i = 0; i < avctx->height; i++)
00142 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
00143 s->x = s->y = 0;
00144 }
00145
00149 static void draw_char(AVCodecContext *avctx, int c)
00150 {
00151 AnsiContext *s = avctx->priv_data;
00152 int fg = s->fg;
00153 int bg = s->bg;
00154
00155 if ((s->attributes & ATTR_BOLD))
00156 fg += 8;
00157 if ((s->attributes & ATTR_BLINK))
00158 bg += 8;
00159 if ((s->attributes & ATTR_REVERSE))
00160 FFSWAP(int, fg, bg);
00161 if ((s->attributes & ATTR_CONCEALED))
00162 fg = bg;
00163 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
00164 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
00165 s->x += FONT_WIDTH;
00166 if (s->x >= avctx->width) {
00167 s->x = 0;
00168 hscroll(avctx);
00169 }
00170 }
00171
00176 static int execute_code(AVCodecContext * avctx, int c)
00177 {
00178 AnsiContext *s = avctx->priv_data;
00179 int ret, i, width, height;
00180 switch(c) {
00181 case 'A':
00182 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
00183 break;
00184 case 'B':
00185 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
00186 break;
00187 case 'C':
00188 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
00189 break;
00190 case 'D':
00191 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
00192 break;
00193 case 'H':
00194 case 'f':
00195 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
00196 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
00197 break;
00198 case 'h':
00199 case 'l':
00200 if (s->nb_args < 2)
00201 s->args[0] = DEFAULT_SCREEN_MODE;
00202 width = avctx->width;
00203 height = avctx->height;
00204 switch(s->args[0]) {
00205 case 0: case 1: case 4: case 5: case 13: case 19:
00206 s->font = ff_cga_font;
00207 s->font_height = 8;
00208 width = 40<<3;
00209 height = 25<<3;
00210 break;
00211 case 2: case 3:
00212 s->font = ff_vga16_font;
00213 s->font_height = 16;
00214 width = 80<<3;
00215 height = 25<<4;
00216 break;
00217 case 6: case 14:
00218 s->font = ff_cga_font;
00219 s->font_height = 8;
00220 width = 80<<3;
00221 height = 25<<3;
00222 break;
00223 case 7:
00224 break;
00225 case 15: case 16:
00226 s->font = ff_cga_font;
00227 s->font_height = 8;
00228 width = 80<<3;
00229 height = 43<<3;
00230 break;
00231 case 17: case 18:
00232 s->font = ff_cga_font;
00233 s->font_height = 8;
00234 width = 80<<3;
00235 height = 60<<4;
00236 break;
00237 default:
00238 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
00239 }
00240 if (width != avctx->width || height != avctx->height) {
00241 if (s->frame.data[0])
00242 avctx->release_buffer(avctx, &s->frame);
00243 avcodec_set_dimensions(avctx, width, height);
00244 ret = avctx->get_buffer(avctx, &s->frame);
00245 if (ret < 0) {
00246 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00247 return ret;
00248 }
00249 s->frame.pict_type = AV_PICTURE_TYPE_I;
00250 s->frame.palette_has_changed = 1;
00251 set_palette((uint32_t *)s->frame.data[1]);
00252 erase_screen(avctx);
00253 } else if (c == 'l') {
00254 erase_screen(avctx);
00255 }
00256 break;
00257 case 'J':
00258 switch (s->args[0]) {
00259 case 0:
00260 erase_line(avctx, s->x, avctx->width - s->x);
00261 if (s->y < avctx->height - s->font_height)
00262 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
00263 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
00264 break;
00265 case 1:
00266 erase_line(avctx, 0, s->x);
00267 if (s->y > 0)
00268 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
00269 break;
00270 case 2:
00271 erase_screen(avctx);
00272 }
00273 break;
00274 case 'K':
00275 switch(s->args[0]) {
00276 case 0:
00277 erase_line(avctx, s->x, avctx->width - s->x);
00278 break;
00279 case 1:
00280 erase_line(avctx, 0, s->x);
00281 break;
00282 case 2:
00283 erase_line(avctx, 0, avctx->width);
00284 }
00285 break;
00286 case 'm':
00287 if (s->nb_args == 0) {
00288 s->nb_args = 1;
00289 s->args[0] = 0;
00290 }
00291 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
00292 int m = s->args[i];
00293 if (m == 0) {
00294 s->attributes = 0;
00295 s->fg = DEFAULT_FG_COLOR;
00296 s->bg = DEFAULT_BG_COLOR;
00297 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
00298 s->attributes |= 1 << (m - 1);
00299 } else if (m >= 30 && m <= 37) {
00300 s->fg = ansi_to_cga[m - 30];
00301 } else if (m == 38 && i + 2 < s->nb_args && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
00302 int index = s->args[i + 2];
00303 s->fg = index < 16 ? ansi_to_cga[index] : index;
00304 i += 2;
00305 } else if (m == 39) {
00306 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
00307 } else if (m >= 40 && m <= 47) {
00308 s->bg = ansi_to_cga[m - 40];
00309 } else if (m == 48 && i + 2 < s->nb_args && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
00310 int index = s->args[i + 2];
00311 s->bg = index < 16 ? ansi_to_cga[index] : index;
00312 i += 2;
00313 } else if (m == 49) {
00314 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
00315 } else {
00316 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
00317 }
00318 }
00319 break;
00320 case 'n':
00321 case 'R':
00322
00323 break;
00324 case 's':
00325 s->sx = s->x;
00326 s->sy = s->y;
00327 break;
00328 case 'u':
00329 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
00330 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
00331 break;
00332 default:
00333 av_log_ask_for_sample(avctx, "unsupported escape code\n");
00334 break;
00335 }
00336 return 0;
00337 }
00338
00339 static int decode_frame(AVCodecContext *avctx,
00340 void *data, int *data_size,
00341 AVPacket *avpkt)
00342 {
00343 AnsiContext *s = avctx->priv_data;
00344 uint8_t *buf = avpkt->data;
00345 int buf_size = avpkt->size;
00346 const uint8_t *buf_end = buf+buf_size;
00347 int ret, i, count;
00348
00349 ret = avctx->reget_buffer(avctx, &s->frame);
00350 if (ret < 0){
00351 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00352 return ret;
00353 }
00354 s->frame.pict_type = AV_PICTURE_TYPE_I;
00355 s->frame.palette_has_changed = 1;
00356 set_palette((uint32_t *)s->frame.data[1]);
00357 if (!s->first_frame) {
00358 erase_screen(avctx);
00359 s->first_frame = 1;
00360 }
00361
00362 while(buf < buf_end) {
00363 switch(s->state) {
00364 case STATE_NORMAL:
00365 switch (buf[0]) {
00366 case 0x00:
00367 case 0x07:
00368 case 0x1A:
00369
00370 break;
00371 case 0x08:
00372 s->x = FFMAX(s->x - 1, 0);
00373 break;
00374 case 0x09:
00375 i = s->x / FONT_WIDTH;
00376 count = ((i + 8) & ~7) - i;
00377 for (i = 0; i < count; i++)
00378 draw_char(avctx, ' ');
00379 break;
00380 case 0x0A:
00381 hscroll(avctx);
00382 case 0x0D:
00383 s->x = 0;
00384 break;
00385 case 0x0C:
00386 erase_screen(avctx);
00387 break;
00388 case 0x1B:
00389 s->state = STATE_ESCAPE;
00390 break;
00391 default:
00392 draw_char(avctx, buf[0]);
00393 }
00394 break;
00395 case STATE_ESCAPE:
00396 if (buf[0] == '[') {
00397 s->state = STATE_CODE;
00398 s->nb_args = 0;
00399 s->args[0] = -1;
00400 } else {
00401 s->state = STATE_NORMAL;
00402 draw_char(avctx, 0x1B);
00403 continue;
00404 }
00405 break;
00406 case STATE_CODE:
00407 switch(buf[0]) {
00408 case '0': case '1': case '2': case '3': case '4':
00409 case '5': case '6': case '7': case '8': case '9':
00410 if (s->nb_args < MAX_NB_ARGS)
00411 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
00412 break;
00413 case ';':
00414 s->nb_args++;
00415 if (s->nb_args < MAX_NB_ARGS)
00416 s->args[s->nb_args] = 0;
00417 break;
00418 case 'M':
00419 s->state = STATE_MUSIC_PREAMBLE;
00420 break;
00421 case '=': case '?':
00422
00423 break;
00424 default:
00425 if (s->nb_args > MAX_NB_ARGS)
00426 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
00427 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
00428 s->nb_args++;
00429 if (execute_code(avctx, buf[0]) < 0)
00430 return -1;
00431 s->state = STATE_NORMAL;
00432 }
00433 break;
00434 case STATE_MUSIC_PREAMBLE:
00435 if (buf[0] == 0x0E || buf[0] == 0x1B)
00436 s->state = STATE_NORMAL;
00437
00438 break;
00439 }
00440 buf++;
00441 }
00442
00443 *data_size = sizeof(AVFrame);
00444 *(AVFrame*)data = s->frame;
00445 return buf_size;
00446 }
00447
00448 static av_cold int decode_close(AVCodecContext *avctx)
00449 {
00450 AnsiContext *s = avctx->priv_data;
00451 if (s->frame.data[0])
00452 avctx->release_buffer(avctx, &s->frame);
00453 return 0;
00454 }
00455
00456 AVCodec ff_ansi_decoder = {
00457 .name = "ansi",
00458 .type = AVMEDIA_TYPE_VIDEO,
00459 .id = AV_CODEC_ID_ANSI,
00460 .priv_data_size = sizeof(AnsiContext),
00461 .init = decode_init,
00462 .close = decode_close,
00463 .decode = decode_frame,
00464 .capabilities = CODEC_CAP_DR1,
00465 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
00466 };