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"
32 #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
33 #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
34 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
35 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
36 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
37 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
39 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
40 #define DEFAULT_BG_COLOR 0
41 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
43 #define FONT_WIDTH 8 /**< Font width */
45 /** map ansi color index to cga palette index */
46 static const uint8_t ansi_to_cga[16] = {
47 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
52 int x; /**< x cursor position (pixels) */
53 int y; /**< y cursor position (pixels) */
54 int sx; /**< saved x cursor position (pixels) */
55 int sy; /**< saved y cursor position (pixels) */
56 const uint8_t* font; /**< font */
57 int font_height; /**< font height */
58 int attributes; /**< attribute flags */
59 int fg; /**< foreground color */
60 int bg; /**< background color */
63 /* ansi parser state machine */
71 int args[MAX_NB_ARGS];
72 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
75 static av_cold int decode_init(AVCodecContext *avctx)
77 AnsiContext *s = avctx->priv_data;
78 avctx->pix_fmt = PIX_FMT_PAL8;
81 s->font = ff_vga16_font;
83 s->fg = DEFAULT_FG_COLOR;
84 s->bg = DEFAULT_BG_COLOR;
86 avcodec_get_frame_defaults(&s->frame);
87 if (!avctx->width || !avctx->height)
88 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
93 static void set_palette(uint32_t *pal)
96 memcpy(pal, ff_cga_palette, 16 * 4);
98 #define COLOR(x) ((x) * 40 + 55)
99 for (r = 0; r < 6; r++)
100 for (g = 0; g < 6; g++)
101 for (b = 0; b < 6; b++)
102 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
103 #define GRAY(x) ((x) * 10 + 8)
104 for (g = 0; g < 24; g++)
105 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
108 static void hscroll(AVCodecContext *avctx)
110 AnsiContext *s = avctx->priv_data;
113 if (s->y < avctx->height - s->font_height) {
114 s->y += s->font_height;
119 for (; i < avctx->height - s->font_height; i++)
120 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
121 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
123 for (; i < avctx->height; i++)
124 memset(s->frame.data[0] + i * s->frame.linesize[0],
125 DEFAULT_BG_COLOR, avctx->width);
128 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
130 AnsiContext *s = avctx->priv_data;
132 for (i = 0; i < s->font_height; i++)
133 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
134 DEFAULT_BG_COLOR, xlength);
137 static void erase_screen(AVCodecContext *avctx)
139 AnsiContext *s = avctx->priv_data;
141 for (i = 0; i < avctx->height; i++)
142 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
147 * Draw character to screen
149 static void draw_char(AVCodecContext *avctx, int c)
151 AnsiContext *s = avctx->priv_data;
155 if ((s->attributes & ATTR_BOLD))
157 if ((s->attributes & ATTR_BLINK))
159 if ((s->attributes & ATTR_REVERSE))
161 if ((s->attributes & ATTR_CONCEALED))
163 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
164 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
166 if (s->x >= avctx->width) {
173 * Execute ANSI escape code
174 * @return 0 on success, negative on error
176 static int execute_code(AVCodecContext * avctx, int c)
178 AnsiContext *s = avctx->priv_data;
179 int ret, i, width, height;
181 case 'A': //Cursor Up
182 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
184 case 'B': //Cursor Down
185 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
187 case 'C': //Cursor Right
188 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
190 case 'D': //Cursor Left
191 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
193 case 'H': //Cursor Position
194 case 'f': //Horizontal and Vertical Position
195 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
196 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
198 case 'h': //set creen mode
199 case 'l': //reset screen mode
201 s->args[0] = DEFAULT_SCREEN_MODE;
202 width = avctx->width;
203 height = avctx->height;
205 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
206 s->font = ff_cga_font;
211 case 2: case 3: //640x400 (25 rows)
212 s->font = ff_vga16_font;
217 case 6: case 14: //640x200 (25 rows)
218 s->font = ff_cga_font;
223 case 7: //set line wrapping
225 case 15: case 16: //640x350 (43 rows)
226 s->font = ff_cga_font;
231 case 17: case 18: //640x480 (60 rows)
232 s->font = ff_cga_font;
238 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
240 if (width != avctx->width || height != avctx->height) {
241 if (s->frame.data[0])
242 avctx->release_buffer(avctx, &s->frame);
243 avcodec_set_dimensions(avctx, width, height);
244 ret = avctx->get_buffer(avctx, &s->frame);
246 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
249 s->frame.pict_type = AV_PICTURE_TYPE_I;
250 s->frame.palette_has_changed = 1;
251 set_palette((uint32_t *)s->frame.data[1]);
253 } else if (c == 'l') {
257 case 'J': //Erase in Page
258 switch (s->args[0]) {
260 erase_line(avctx, s->x, avctx->width - s->x);
261 if (s->y < avctx->height - s->font_height)
262 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
263 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
266 erase_line(avctx, 0, s->x);
268 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
274 case 'K': //Erase in Line
277 erase_line(avctx, s->x, avctx->width - s->x);
280 erase_line(avctx, 0, s->x);
283 erase_line(avctx, 0, avctx->width);
286 case 'm': //Select Graphics Rendition
287 if (s->nb_args == 0) {
291 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
295 s->fg = DEFAULT_FG_COLOR;
296 s->bg = DEFAULT_BG_COLOR;
297 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
298 s->attributes |= 1 << (m - 1);
299 } else if (m >= 30 && m <= 37) {
300 s->fg = ansi_to_cga[m - 30];
301 } else if (m == 38 && i + 2 < s->nb_args && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
302 int index = s->args[i + 2];
303 s->fg = index < 16 ? ansi_to_cga[index] : index;
305 } else if (m == 39) {
306 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
307 } else if (m >= 40 && m <= 47) {
308 s->bg = ansi_to_cga[m - 40];
309 } else if (m == 48 && i + 2 < s->nb_args && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
310 int index = s->args[i + 2];
311 s->bg = index < 16 ? ansi_to_cga[index] : index;
313 } else if (m == 49) {
314 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
316 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
320 case 'n': //Device Status Report
321 case 'R': //report current line and column
324 case 's': //Save Cursor Position
328 case 'u': //Restore Cursor Position
329 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
330 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
333 av_log_ask_for_sample(avctx, "unsupported escape code\n");
339 static int decode_frame(AVCodecContext *avctx,
340 void *data, int *data_size,
343 AnsiContext *s = avctx->priv_data;
344 uint8_t *buf = avpkt->data;
345 int buf_size = avpkt->size;
346 const uint8_t *buf_end = buf+buf_size;
349 ret = avctx->reget_buffer(avctx, &s->frame);
351 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
354 s->frame.pict_type = AV_PICTURE_TYPE_I;
355 s->frame.palette_has_changed = 1;
356 set_palette((uint32_t *)s->frame.data[1]);
357 if (!s->first_frame) {
362 while(buf < buf_end) {
372 s->x = FFMAX(s->x - 1, 0);
375 i = s->x / FONT_WIDTH;
376 count = ((i + 8) & ~7) - i;
377 for (i = 0; i < count; i++)
378 draw_char(avctx, ' ');
389 s->state = STATE_ESCAPE;
392 draw_char(avctx, buf[0]);
397 s->state = STATE_CODE;
401 s->state = STATE_NORMAL;
402 draw_char(avctx, 0x1B);
408 case '0': case '1': case '2': case '3': case '4':
409 case '5': case '6': case '7': case '8': case '9':
410 if (s->nb_args < MAX_NB_ARGS)
411 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
415 if (s->nb_args < MAX_NB_ARGS)
416 s->args[s->nb_args] = 0;
419 s->state = STATE_MUSIC_PREAMBLE;
425 if (s->nb_args > MAX_NB_ARGS)
426 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
427 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
429 if (execute_code(avctx, buf[0]) < 0)
431 s->state = STATE_NORMAL;
434 case STATE_MUSIC_PREAMBLE:
435 if (buf[0] == 0x0E || buf[0] == 0x1B)
436 s->state = STATE_NORMAL;
437 /* ignore music data */
443 *data_size = sizeof(AVFrame);
444 *(AVFrame*)data = s->frame;
448 static av_cold int decode_close(AVCodecContext *avctx)
450 AnsiContext *s = avctx->priv_data;
451 if (s->frame.data[0])
452 avctx->release_buffer(avctx, &s->frame);
456 AVCodec ff_ansi_decoder = {
458 .type = AVMEDIA_TYPE_VIDEO,
459 .id = AV_CODEC_ID_ANSI,
460 .priv_data_size = sizeof(AnsiContext),
462 .close = decode_close,
463 .decode = decode_frame,
464 .capabilities = CODEC_CAP_DR1,
465 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),