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