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/lfg.h"
31 #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
32 #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
33 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
34 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
35 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
36 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
38 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
39 #define DEFAULT_BG_COLOR 0
40 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
42 #define FONT_WIDTH 8 /**< Font width */
44 /** map ansi color index to cga palette index */
45 static const uint8_t ansi_to_cga[16] = {
46 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
51 int x; /**< x cursor position (pixels) */
52 int y; /**< y cursor position (pixels) */
53 int sx; /**< saved x cursor position (pixels) */
54 int sy; /**< saved y cursor position (pixels) */
55 const uint8_t* font; /**< font */
56 int font_height; /**< font height */
57 int attributes; /**< attribute flags */
58 int fg; /**< foreground color */
59 int bg; /**< background color */
61 /* ansi parser state machine */
69 int args[MAX_NB_ARGS];
70 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
73 static av_cold int decode_init(AVCodecContext *avctx)
75 AnsiContext *s = avctx->priv_data;
76 avctx->pix_fmt = PIX_FMT_PAL8;
79 s->font = ff_vga16_font;
81 s->fg = DEFAULT_FG_COLOR;
82 s->bg = DEFAULT_BG_COLOR;
84 if (!avctx->width || !avctx->height)
85 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
90 static void hscroll(AVCodecContext *avctx)
92 AnsiContext *s = avctx->priv_data;
95 if (s->y < avctx->height - s->font_height) {
96 s->y += s->font_height;
101 for (; i < avctx->height - s->font_height; i++)
102 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
103 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
105 for (; i < avctx->height; i++)
106 memset(s->frame.data[0] + i * s->frame.linesize[0],
107 DEFAULT_BG_COLOR, avctx->width);
110 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
112 AnsiContext *s = avctx->priv_data;
114 for (i = 0; i < s->font_height; i++)
115 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
116 DEFAULT_BG_COLOR, xlength);
119 static void erase_screen(AVCodecContext *avctx)
121 AnsiContext *s = avctx->priv_data;
123 for (i = 0; i < avctx->height; i++)
124 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
129 * Draw character to screen
131 static void draw_char(AVCodecContext *avctx, int c)
133 AnsiContext *s = avctx->priv_data;
137 if ((s->attributes & ATTR_BOLD))
139 if ((s->attributes & ATTR_BLINK))
141 if ((s->attributes & ATTR_REVERSE))
143 if ((s->attributes & ATTR_CONCEALED))
145 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
146 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
148 if (s->x >= avctx->width) {
155 * Execute ANSI escape code
158 static int execute_code(AVCodecContext * avctx, int c)
160 AnsiContext *s = avctx->priv_data;
161 int ret, i, width, height;
163 case 'A': //Cursor Up
164 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
166 case 'B': //Cursor Down
167 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
169 case 'C': //Cursor Right
170 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
172 case 'D': //Cursor Left
173 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
175 case 'H': //Cursor Position
176 case 'f': //Horizontal and Vertical Position
177 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
178 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
180 case 'h': //set creen mode
181 case 'l': //reset screen mode
183 s->args[0] = DEFAULT_SCREEN_MODE;
185 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
186 s->font = ff_cga_font;
191 case 2: case 3: //640x400 (25 rows)
192 s->font = ff_vga16_font;
197 case 6: case 14: //640x200 (25 rows)
198 s->font = ff_cga_font;
203 case 7: //set line wrapping
205 case 15: case 16: //640x350 (43 rows)
206 s->font = ff_cga_font;
211 case 17: case 18: //640x480 (60 rows)
212 s->font = ff_cga_font;
218 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
220 if (width != avctx->width || height != avctx->height) {
221 if (s->frame.data[0])
222 avctx->release_buffer(avctx, &s->frame);
223 avcodec_set_dimensions(avctx, width, height);
224 ret = avctx->get_buffer(avctx, &s->frame);
226 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
229 s->frame.pict_type = FF_I_TYPE;
230 s->frame.palette_has_changed = 1;
231 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
233 } else if (c == 'l') {
237 case 'J': //Erase in Page
238 switch (s->args[0]) {
240 erase_line(avctx, s->x, avctx->width - s->x);
241 if (s->y < avctx->height - s->font_height)
242 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
243 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
246 erase_line(avctx, 0, s->x);
248 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
254 case 'K': //Erase in Line
257 erase_line(avctx, s->x, avctx->width - s->x);
260 erase_line(avctx, 0, s->x);
263 erase_line(avctx, 0, avctx->width);
266 case 'm': //Select Graphics Rendition
267 if (s->nb_args == 0) {
271 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
275 s->fg = DEFAULT_FG_COLOR;
276 s->bg = DEFAULT_BG_COLOR;
277 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
278 s->attributes |= 1 << (m - 1);
279 } else if (m >= 30 && m <= 38) {
280 s->fg = ansi_to_cga[m - 30];
281 } else if (m == 39) {
282 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
283 } else if (m >= 40 && m <= 47) {
284 s->bg = ansi_to_cga[m - 40];
285 } else if (m == 49) {
286 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
288 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
292 case 'n': //Device Status Report
293 case 'R': //report current line and column
296 case 's': //Save Cursor Position
300 case 'u': //Restore Cursor Position
301 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
302 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
305 av_log_ask_for_sample(avctx, "unsupported escape code\n");
311 static int decode_frame(AVCodecContext *avctx,
312 void *data, int *data_size,
315 AnsiContext *s = avctx->priv_data;
316 uint8_t *buf = avpkt->data;
317 int buf_size = avpkt->size;
318 const uint8_t *buf_end = buf+buf_size;
321 ret = avctx->reget_buffer(avctx, &s->frame);
323 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
326 s->frame.pict_type = FF_I_TYPE;
327 s->frame.palette_has_changed = 1;
328 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
330 while(buf < buf_end) {
340 s->x = FFMAX(s->x - 1, 0);
343 i = s->x / FONT_WIDTH;
344 count = ((i + 8) & ~7) - i;
345 for (i = 0; i < count; i++)
346 draw_char(avctx, ' ');
357 s->state = STATE_ESCAPE;
360 draw_char(avctx, buf[0]);
365 s->state = STATE_CODE;
369 s->state = STATE_NORMAL;
370 draw_char(avctx, 0x1B);
377 case '0': case '1': case '2': case '3': case '4':
378 case '5': case '6': case '7': case '8': case '9':
379 if (s->nb_args < MAX_NB_ARGS)
380 s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
384 if (s->nb_args < MAX_NB_ARGS)
385 s->args[s->nb_args] = 0;
388 s->state = STATE_MUSIC_PREAMBLE;
394 if (s->nb_args > MAX_NB_ARGS)
395 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
396 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
398 if (execute_code(avctx, buf[0]) < 0)
400 s->state = STATE_NORMAL;
403 case STATE_MUSIC_PREAMBLE:
404 if (buf[0] == 0x0E || buf[0] == 0x1B)
405 s->state = STATE_NORMAL;
406 /* ignore music data */
412 *data_size = sizeof(AVFrame);
413 *(AVFrame*)data = s->frame;
417 static av_cold int decode_close(AVCodecContext *avctx)
419 AnsiContext *s = avctx->priv_data;
420 if (s->frame.data[0])
421 avctx->release_buffer(avctx, &s->frame);
425 AVCodec ff_ansi_decoder = {
427 .type = AVMEDIA_TYPE_VIDEO,
429 .priv_data_size = sizeof(AnsiContext),
431 .close = decode_close,
432 .decode = decode_frame,
433 .capabilities = CODEC_CAP_DR1,
434 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),