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 switch(s->args[0]) {
00186 case 0: case 1: case 4: case 5: case 13: case 19:
00187 s->font = ff_cga_font;
00188 s->font_height = 8;
00189 width = 40<<3;
00190 height = 25<<3;
00191 break;
00192 case 2: case 3:
00193 s->font = ff_vga16_font;
00194 s->font_height = 16;
00195 width = 80<<3;
00196 height = 25<<4;
00197 break;
00198 case 6: case 14:
00199 s->font = ff_cga_font;
00200 s->font_height = 8;
00201 width = 80<<3;
00202 height = 25<<3;
00203 break;
00204 case 7:
00205 break;
00206 case 15: case 16:
00207 s->font = ff_cga_font;
00208 s->font_height = 8;
00209 width = 80<<3;
00210 height = 43<<3;
00211 break;
00212 case 17: case 18:
00213 s->font = ff_cga_font;
00214 s->font_height = 8;
00215 width = 80<<3;
00216 height = 60<<4;
00217 break;
00218 default:
00219 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
00220 }
00221 if (width != avctx->width || height != avctx->height) {
00222 if (s->frame.data[0])
00223 avctx->release_buffer(avctx, &s->frame);
00224 avcodec_set_dimensions(avctx, width, height);
00225 ret = avctx->get_buffer(avctx, &s->frame);
00226 if (ret < 0) {
00227 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00228 return ret;
00229 }
00230 s->frame.pict_type = AV_PICTURE_TYPE_I;
00231 s->frame.palette_has_changed = 1;
00232 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
00233 erase_screen(avctx);
00234 } else if (c == 'l') {
00235 erase_screen(avctx);
00236 }
00237 break;
00238 case 'J':
00239 switch (s->args[0]) {
00240 case 0:
00241 erase_line(avctx, s->x, avctx->width - s->x);
00242 if (s->y < avctx->height - s->font_height)
00243 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
00244 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
00245 break;
00246 case 1:
00247 erase_line(avctx, 0, s->x);
00248 if (s->y > 0)
00249 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
00250 break;
00251 case 2:
00252 erase_screen(avctx);
00253 }
00254 break;
00255 case 'K':
00256 switch(s->args[0]) {
00257 case 0:
00258 erase_line(avctx, s->x, avctx->width - s->x);
00259 break;
00260 case 1:
00261 erase_line(avctx, 0, s->x);
00262 break;
00263 case 2:
00264 erase_line(avctx, 0, avctx->width);
00265 }
00266 break;
00267 case 'm':
00268 if (s->nb_args == 0) {
00269 s->nb_args = 1;
00270 s->args[0] = 0;
00271 }
00272 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
00273 int m = s->args[i];
00274 if (m == 0) {
00275 s->attributes = 0;
00276 s->fg = DEFAULT_FG_COLOR;
00277 s->bg = DEFAULT_BG_COLOR;
00278 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
00279 s->attributes |= 1 << (m - 1);
00280 } else if (m >= 30 && m <= 38) {
00281 s->fg = ansi_to_cga[m - 30];
00282 } else if (m == 39) {
00283 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
00284 } else if (m >= 40 && m <= 47) {
00285 s->bg = ansi_to_cga[m - 40];
00286 } else if (m == 49) {
00287 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
00288 } else {
00289 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
00290 }
00291 }
00292 break;
00293 case 'n':
00294 case 'R':
00295
00296 break;
00297 case 's':
00298 s->sx = s->x;
00299 s->sy = s->y;
00300 break;
00301 case 'u':
00302 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
00303 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
00304 break;
00305 default:
00306 av_log_ask_for_sample(avctx, "unsupported escape code\n");
00307 break;
00308 }
00309 return 0;
00310 }
00311
00312 static int decode_frame(AVCodecContext *avctx,
00313 void *data, int *data_size,
00314 AVPacket *avpkt)
00315 {
00316 AnsiContext *s = avctx->priv_data;
00317 uint8_t *buf = avpkt->data;
00318 int buf_size = avpkt->size;
00319 const uint8_t *buf_end = buf+buf_size;
00320 int ret, i, count;
00321
00322 ret = avctx->reget_buffer(avctx, &s->frame);
00323 if (ret < 0){
00324 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00325 return ret;
00326 }
00327 s->frame.pict_type = AV_PICTURE_TYPE_I;
00328 s->frame.palette_has_changed = 1;
00329 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
00330
00331 while(buf < buf_end) {
00332 switch(s->state) {
00333 case STATE_NORMAL:
00334 switch (buf[0]) {
00335 case 0x00:
00336 case 0x07:
00337 case 0x1A:
00338
00339 break;
00340 case 0x08:
00341 s->x = FFMAX(s->x - 1, 0);
00342 break;
00343 case 0x09:
00344 i = s->x / FONT_WIDTH;
00345 count = ((i + 8) & ~7) - i;
00346 for (i = 0; i < count; i++)
00347 draw_char(avctx, ' ');
00348 break;
00349 case 0x0A:
00350 hscroll(avctx);
00351 case 0x0D:
00352 s->x = 0;
00353 break;
00354 case 0x0C:
00355 erase_screen(avctx);
00356 break;
00357 case 0x1B:
00358 s->state = STATE_ESCAPE;
00359 break;
00360 default:
00361 draw_char(avctx, buf[0]);
00362 }
00363 break;
00364 case STATE_ESCAPE:
00365 if (buf[0] == '[') {
00366 s->state = STATE_CODE;
00367 s->nb_args = 0;
00368 s->args[0] = 0;
00369 } else {
00370 s->state = STATE_NORMAL;
00371 draw_char(avctx, 0x1B);
00372 return -1;
00373 continue;
00374 }
00375 break;
00376 case STATE_CODE:
00377 switch(buf[0]) {
00378 case '0': case '1': case '2': case '3': case '4':
00379 case '5': case '6': case '7': case '8': case '9':
00380 if (s->nb_args < MAX_NB_ARGS)
00381 s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
00382 break;
00383 case ';':
00384 s->nb_args++;
00385 if (s->nb_args < MAX_NB_ARGS)
00386 s->args[s->nb_args] = 0;
00387 break;
00388 case 'M':
00389 s->state = STATE_MUSIC_PREAMBLE;
00390 break;
00391 case '=': case '?':
00392
00393 break;
00394 default:
00395 if (s->nb_args > MAX_NB_ARGS)
00396 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
00397 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
00398 s->nb_args++;
00399 if (execute_code(avctx, buf[0]) < 0)
00400 return -1;
00401 s->state = STATE_NORMAL;
00402 }
00403 break;
00404 case STATE_MUSIC_PREAMBLE:
00405 if (buf[0] == 0x0E || buf[0] == 0x1B)
00406 s->state = STATE_NORMAL;
00407
00408 break;
00409 }
00410 buf++;
00411 }
00412
00413 *data_size = sizeof(AVFrame);
00414 *(AVFrame*)data = s->frame;
00415 return buf_size;
00416 }
00417
00418 static av_cold int decode_close(AVCodecContext *avctx)
00419 {
00420 AnsiContext *s = avctx->priv_data;
00421 if (s->frame.data[0])
00422 avctx->release_buffer(avctx, &s->frame);
00423 return 0;
00424 }
00425
00426 AVCodec ff_ansi_decoder = {
00427 .name = "ansi",
00428 .type = AVMEDIA_TYPE_VIDEO,
00429 .id = CODEC_ID_ANSI,
00430 .priv_data_size = sizeof(AnsiContext),
00431 .init = decode_init,
00432 .close = decode_close,
00433 .decode = decode_frame,
00434 .capabilities = CODEC_CAP_DR1,
00435 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
00436 };