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/lfg.h"
29 #include "libavutil/xga_font_data.h"
34 #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
35 #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
36 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
37 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
38 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
39 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
41 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
42 #define DEFAULT_BG_COLOR 0
43 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
45 #define FONT_WIDTH 8 /**< Font width */
47 /** map ansi color index to cga palette index */
48 static const uint8_t ansi_to_cga[16] = {
49 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
54 int x; /**< x cursor position (pixels) */
55 int y; /**< y cursor position (pixels) */
56 int sx; /**< saved x cursor position (pixels) */
57 int sy; /**< saved y cursor position (pixels) */
58 const uint8_t* font; /**< font */
59 int font_height; /**< font height */
60 int attributes; /**< attribute flags */
61 int fg; /**< foreground color */
62 int bg; /**< background color */
65 /* ansi parser state machine */
73 int args[MAX_NB_ARGS];
74 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
77 static av_cold int decode_init(AVCodecContext *avctx)
79 AnsiContext *s = avctx->priv_data;
80 avctx->pix_fmt = AV_PIX_FMT_PAL8;
83 s->font = avpriv_vga16_font;
85 s->fg = DEFAULT_FG_COLOR;
86 s->bg = DEFAULT_BG_COLOR;
88 avcodec_get_frame_defaults(&s->frame);
89 if (!avctx->width || !avctx->height)
90 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
95 static void set_palette(uint32_t *pal)
98 memcpy(pal, ff_cga_palette, 16 * 4);
100 #define COLOR(x) ((x) * 40 + 55)
101 for (r = 0; r < 6; r++)
102 for (g = 0; g < 6; g++)
103 for (b = 0; b < 6; b++)
104 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
105 #define GRAY(x) ((x) * 10 + 8)
106 for (g = 0; g < 24; g++)
107 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
110 static void hscroll(AVCodecContext *avctx)
112 AnsiContext *s = avctx->priv_data;
115 if (s->y < avctx->height - s->font_height) {
116 s->y += s->font_height;
121 for (; i < avctx->height - s->font_height; i++)
122 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
123 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
125 for (; i < avctx->height; i++)
126 memset(s->frame.data[0] + i * s->frame.linesize[0],
127 DEFAULT_BG_COLOR, avctx->width);
130 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
132 AnsiContext *s = avctx->priv_data;
134 for (i = 0; i < s->font_height; i++)
135 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
136 DEFAULT_BG_COLOR, xlength);
139 static void erase_screen(AVCodecContext *avctx)
141 AnsiContext *s = avctx->priv_data;
143 for (i = 0; i < avctx->height; i++)
144 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
149 * Draw character to screen
151 static void draw_char(AVCodecContext *avctx, int c)
153 AnsiContext *s = avctx->priv_data;
157 if ((s->attributes & ATTR_BOLD))
159 if ((s->attributes & ATTR_BLINK))
161 if ((s->attributes & ATTR_REVERSE))
163 if ((s->attributes & ATTR_CONCEALED))
165 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
166 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
168 if (s->x >= avctx->width) {
175 * Execute ANSI escape code
176 * @return 0 on success, negative on error
178 static int execute_code(AVCodecContext * avctx, int c)
180 AnsiContext *s = avctx->priv_data;
181 int ret, i, width, height;
183 case 'A': //Cursor Up
184 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
186 case 'B': //Cursor Down
187 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
189 case 'C': //Cursor Right
190 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
192 case 'D': //Cursor Left
193 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
195 case 'H': //Cursor Position
196 case 'f': //Horizontal and Vertical Position
197 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
198 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
200 case 'h': //set creen mode
201 case 'l': //reset screen mode
203 s->args[0] = DEFAULT_SCREEN_MODE;
204 width = avctx->width;
205 height = avctx->height;
207 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
208 s->font = avpriv_cga_font;
213 case 2: case 3: //640x400 (25 rows)
214 s->font = avpriv_vga16_font;
219 case 6: case 14: //640x200 (25 rows)
220 s->font = avpriv_cga_font;
225 case 7: //set line wrapping
227 case 15: case 16: //640x350 (43 rows)
228 s->font = avpriv_cga_font;
233 case 17: case 18: //640x480 (60 rows)
234 s->font = avpriv_cga_font;
240 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
242 if (width != avctx->width || height != avctx->height) {
243 if (s->frame.data[0])
244 avctx->release_buffer(avctx, &s->frame);
245 avcodec_set_dimensions(avctx, width, height);
246 ret = ff_get_buffer(avctx, &s->frame);
248 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
251 s->frame.pict_type = AV_PICTURE_TYPE_I;
252 s->frame.palette_has_changed = 1;
253 set_palette((uint32_t *)s->frame.data[1]);
255 } else if (c == 'l') {
259 case 'J': //Erase in Page
260 switch (s->args[0]) {
262 erase_line(avctx, s->x, avctx->width - s->x);
263 if (s->y < avctx->height - s->font_height)
264 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
265 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
268 erase_line(avctx, 0, s->x);
270 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
276 case 'K': //Erase in Line
279 erase_line(avctx, s->x, avctx->width - s->x);
282 erase_line(avctx, 0, s->x);
285 erase_line(avctx, 0, avctx->width);
288 case 'm': //Select Graphics Rendition
289 if (s->nb_args == 0) {
293 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
297 s->fg = DEFAULT_FG_COLOR;
298 s->bg = DEFAULT_BG_COLOR;
299 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
300 s->attributes |= 1 << (m - 1);
301 } else if (m >= 30 && m <= 37) {
302 s->fg = ansi_to_cga[m - 30];
303 } else if (m == 38 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
304 int index = s->args[i + 2];
305 s->fg = index < 16 ? ansi_to_cga[index] : index;
307 } else if (m == 39) {
308 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
309 } else if (m >= 40 && m <= 47) {
310 s->bg = ansi_to_cga[m - 40];
311 } else if (m == 48 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
312 int index = s->args[i + 2];
313 s->bg = index < 16 ? ansi_to_cga[index] : index;
315 } else if (m == 49) {
316 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
318 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
322 case 'n': //Device Status Report
323 case 'R': //report current line and column
326 case 's': //Save Cursor Position
330 case 'u': //Restore Cursor Position
331 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
332 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
335 av_log_ask_for_sample(avctx, "unsupported escape code\n");
341 static int decode_frame(AVCodecContext *avctx,
342 void *data, int *got_frame,
345 AnsiContext *s = avctx->priv_data;
346 uint8_t *buf = avpkt->data;
347 int buf_size = avpkt->size;
348 const uint8_t *buf_end = buf+buf_size;
351 ret = avctx->reget_buffer(avctx, &s->frame);
353 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
356 if (!avctx->frame_number) {
357 for (i=0; i<avctx->height; i++)
358 memset(s->frame.data[0]+ i*s->frame.linesize[0], 0, avctx->width);
359 memset(s->frame.data[1], 0, AVPALETTE_SIZE);
362 s->frame.pict_type = AV_PICTURE_TYPE_I;
363 s->frame.palette_has_changed = 1;
364 set_palette((uint32_t *)s->frame.data[1]);
365 if (!s->first_frame) {
370 while(buf < buf_end) {
380 s->x = FFMAX(s->x - 1, 0);
383 i = s->x / FONT_WIDTH;
384 count = ((i + 8) & ~7) - i;
385 for (i = 0; i < count; i++)
386 draw_char(avctx, ' ');
397 s->state = STATE_ESCAPE;
400 draw_char(avctx, buf[0]);
405 s->state = STATE_CODE;
409 s->state = STATE_NORMAL;
410 draw_char(avctx, 0x1B);
416 case '0': case '1': case '2': case '3': case '4':
417 case '5': case '6': case '7': case '8': case '9':
418 if (s->nb_args < MAX_NB_ARGS)
419 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
423 if (s->nb_args < MAX_NB_ARGS)
424 s->args[s->nb_args] = 0;
427 s->state = STATE_MUSIC_PREAMBLE;
433 if (s->nb_args > MAX_NB_ARGS)
434 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
435 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
437 if ((ret = execute_code(avctx, buf[0])) < 0)
439 s->state = STATE_NORMAL;
442 case STATE_MUSIC_PREAMBLE:
443 if (buf[0] == 0x0E || buf[0] == 0x1B)
444 s->state = STATE_NORMAL;
445 /* ignore music data */
452 *(AVFrame*)data = s->frame;
456 static av_cold int decode_close(AVCodecContext *avctx)
458 AnsiContext *s = avctx->priv_data;
459 if (s->frame.data[0])
460 avctx->release_buffer(avctx, &s->frame);
464 AVCodec ff_ansi_decoder = {
466 .type = AVMEDIA_TYPE_VIDEO,
467 .id = AV_CODEC_ID_ANSI,
468 .priv_data_size = sizeof(AnsiContext),
470 .close = decode_close,
471 .decode = decode_frame,
472 .capabilities = CODEC_CAP_DR1,
473 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),