2 * ASCII/ANSI art decoder
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
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 * ASCII/ANSI art decoder
27 #include "libavutil/common.h"
28 #include "libavutil/frame.h"
29 #include "libavutil/lfg.h"
30 #include "libavutil/xga_font_data.h"
35 #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
36 #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
37 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
38 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
39 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
40 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
42 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
43 #define DEFAULT_BG_COLOR 0
44 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
46 #define FONT_WIDTH 8 /**< Font width */
48 /** map ansi color index to cga palette index */
49 static const uint8_t ansi_to_cga[16] = {
50 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
53 typedef struct AnsiContext {
55 int x; /**< x cursor position (pixels) */
56 int y; /**< y cursor position (pixels) */
57 int sx; /**< saved x cursor position (pixels) */
58 int sy; /**< saved y cursor position (pixels) */
59 const uint8_t* font; /**< font */
60 int font_height; /**< font height */
61 int attributes; /**< attribute flags */
62 int fg; /**< foreground color */
63 int bg; /**< background color */
66 /* ansi parser state machine */
74 int args[MAX_NB_ARGS];
75 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
78 static av_cold int decode_init(AVCodecContext *avctx)
80 AnsiContext *s = avctx->priv_data;
81 avctx->pix_fmt = AV_PIX_FMT_PAL8;
84 s->font = avpriv_vga16_font;
86 s->fg = DEFAULT_FG_COLOR;
87 s->bg = DEFAULT_BG_COLOR;
89 if (!avctx->width || !avctx->height) {
90 int ret = ff_set_dimensions(avctx, 80 << 3, 25 << 4);
93 } else if (avctx->width % FONT_WIDTH || avctx->height % s->font_height) {
94 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions %d %d\n", avctx->width, avctx->height);
95 return AVERROR(EINVAL);
98 s->frame = av_frame_alloc();
100 return AVERROR(ENOMEM);
105 static void set_palette(uint32_t *pal)
108 memcpy(pal, ff_cga_palette, 16 * 4);
110 #define COLOR(x) ((x) * 40 + 55)
111 for (r = 0; r < 6; r++)
112 for (g = 0; g < 6; g++)
113 for (b = 0; b < 6; b++)
114 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
115 #define GRAY(x) ((x) * 10 + 8)
116 for (g = 0; g < 24; g++)
117 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
120 static void hscroll(AVCodecContext *avctx)
122 AnsiContext *s = avctx->priv_data;
125 if (s->y <= avctx->height - 2*s->font_height) {
126 s->y += s->font_height;
131 for (; i < avctx->height - s->font_height; i++)
132 memcpy(s->frame->data[0] + i * s->frame->linesize[0],
133 s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
135 for (; i < avctx->height; i++)
136 memset(s->frame->data[0] + i * s->frame->linesize[0],
137 DEFAULT_BG_COLOR, avctx->width);
140 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
142 AnsiContext *s = avctx->priv_data;
144 for (i = 0; i < s->font_height; i++)
145 memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
146 DEFAULT_BG_COLOR, xlength);
149 static void erase_screen(AVCodecContext *avctx)
151 AnsiContext *s = avctx->priv_data;
153 for (i = 0; i < avctx->height; i++)
154 memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
159 * Draw character to screen
161 static void draw_char(AVCodecContext *avctx, int c)
163 AnsiContext *s = avctx->priv_data;
167 if ((s->attributes & ATTR_BOLD))
169 if ((s->attributes & ATTR_BLINK))
171 if ((s->attributes & ATTR_REVERSE))
173 if ((s->attributes & ATTR_CONCEALED))
175 ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
176 s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
178 if (s->x > avctx->width - FONT_WIDTH) {
185 * Execute ANSI escape code
186 * @return 0 on success, negative on error
188 static int execute_code(AVCodecContext * avctx, int c)
190 AnsiContext *s = avctx->priv_data;
192 int width = avctx->width;
193 int height = avctx->height;
196 case 'A': //Cursor Up
197 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
199 case 'B': //Cursor Down
200 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
202 case 'C': //Cursor Right
203 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
205 case 'D': //Cursor Left
206 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
208 case 'H': //Cursor Position
209 case 'f': //Horizontal and Vertical Position
210 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
211 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
213 case 'h': //set screen mode
214 case 'l': //reset screen mode
216 s->args[0] = DEFAULT_SCREEN_MODE;
218 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
219 s->font = avpriv_cga_font;
224 case 2: case 3: //640x400 (25 rows)
225 s->font = avpriv_vga16_font;
230 case 6: case 14: //640x200 (25 rows)
231 s->font = avpriv_cga_font;
236 case 7: //set line wrapping
238 case 15: case 16: //640x350 (43 rows)
239 s->font = avpriv_cga_font;
244 case 17: case 18: //640x480 (60 rows)
245 s->font = avpriv_cga_font;
251 avpriv_request_sample(avctx, "Unsupported screen mode");
253 s->x = av_clip(s->x, 0, width - FONT_WIDTH);
254 s->y = av_clip(s->y, 0, height - s->font_height);
255 if (width != avctx->width || height != avctx->height) {
256 av_frame_unref(s->frame);
257 ret = ff_set_dimensions(avctx, width, height);
260 if ((ret = ff_get_buffer(avctx, s->frame,
261 AV_GET_BUFFER_FLAG_REF)) < 0)
263 s->frame->pict_type = AV_PICTURE_TYPE_I;
264 s->frame->palette_has_changed = 1;
265 set_palette((uint32_t *)s->frame->data[1]);
267 } else if (c == 'l') {
271 case 'J': //Erase in Page
272 switch (s->args[0]) {
274 erase_line(avctx, s->x, avctx->width - s->x);
275 if (s->y < avctx->height - s->font_height)
276 memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
277 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
280 erase_line(avctx, 0, s->x);
282 memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
288 case 'K': //Erase in Line
291 erase_line(avctx, s->x, avctx->width - s->x);
294 erase_line(avctx, 0, s->x);
297 erase_line(avctx, 0, avctx->width);
300 case 'm': //Select Graphics Rendition
301 if (s->nb_args == 0) {
305 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
309 s->fg = DEFAULT_FG_COLOR;
310 s->bg = DEFAULT_BG_COLOR;
311 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
312 s->attributes |= 1 << (m - 1);
313 } else if (m >= 30 && m <= 37) {
314 s->fg = ansi_to_cga[m - 30];
315 } else if (m == 38 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
316 int index = s->args[i + 2];
317 s->fg = index < 16 ? ansi_to_cga[index] : index;
319 } else if (m == 39) {
320 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
321 } else if (m >= 40 && m <= 47) {
322 s->bg = ansi_to_cga[m - 40];
323 } else if (m == 48 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
324 int index = s->args[i + 2];
325 s->bg = index < 16 ? ansi_to_cga[index] : index;
327 } else if (m == 49) {
328 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
330 avpriv_request_sample(avctx, "Unsupported rendition parameter");
334 case 'n': //Device Status Report
335 case 'R': //report current line and column
338 case 's': //Save Cursor Position
342 case 'u': //Restore Cursor Position
343 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
344 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
347 avpriv_request_sample(avctx, "Unknown escape code");
350 s->x = av_clip(s->x, 0, avctx->width - FONT_WIDTH);
351 s->y = av_clip(s->y, 0, avctx->height - s->font_height);
355 static int decode_frame(AVCodecContext *avctx,
356 void *data, int *got_frame,
359 AnsiContext *s = avctx->priv_data;
360 uint8_t *buf = avpkt->data;
361 int buf_size = avpkt->size;
362 const uint8_t *buf_end = buf+buf_size;
365 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
367 if (!avctx->frame_number) {
368 for (i=0; i<avctx->height; i++)
369 memset(s->frame->data[0]+ i*s->frame->linesize[0], 0, avctx->width);
370 memset(s->frame->data[1], 0, AVPALETTE_SIZE);
373 s->frame->pict_type = AV_PICTURE_TYPE_I;
374 s->frame->palette_has_changed = 1;
375 set_palette((uint32_t *)s->frame->data[1]);
376 if (!s->first_frame) {
381 while(buf < buf_end) {
391 s->x = FFMAX(s->x - 1, 0);
394 i = s->x / FONT_WIDTH;
395 count = ((i + 8) & ~7) - i;
396 for (i = 0; i < count; i++)
397 draw_char(avctx, ' ');
408 s->state = STATE_ESCAPE;
411 draw_char(avctx, buf[0]);
416 s->state = STATE_CODE;
420 s->state = STATE_NORMAL;
421 draw_char(avctx, 0x1B);
427 case '0': case '1': case '2': case '3': case '4':
428 case '5': case '6': case '7': case '8': case '9':
429 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] < 6553)
430 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
434 if (s->nb_args < MAX_NB_ARGS)
435 s->args[s->nb_args] = 0;
438 s->state = STATE_MUSIC_PREAMBLE;
444 if (s->nb_args > MAX_NB_ARGS)
445 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
446 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
448 if ((ret = execute_code(avctx, buf[0])) < 0)
450 s->state = STATE_NORMAL;
453 case STATE_MUSIC_PREAMBLE:
454 if (buf[0] == 0x0E || buf[0] == 0x1B)
455 s->state = STATE_NORMAL;
456 /* ignore music data */
463 if ((ret = av_frame_ref(data, s->frame)) < 0)
468 static av_cold int decode_close(AVCodecContext *avctx)
470 AnsiContext *s = avctx->priv_data;
472 av_frame_free(&s->frame);
476 AVCodec ff_ansi_decoder = {
478 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
479 .type = AVMEDIA_TYPE_VIDEO,
480 .id = AV_CODEC_ID_ANSI,
481 .priv_data_size = sizeof(AnsiContext),
483 .close = decode_close,
484 .decode = decode_frame,
485 .capabilities = AV_CODEC_CAP_DR1,
486 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,