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;
83 s->frame = av_frame_alloc();
85 return AVERROR(ENOMEM);
88 s->font = avpriv_vga16_font;
90 s->fg = DEFAULT_FG_COLOR;
91 s->bg = DEFAULT_BG_COLOR;
93 if (!avctx->width || !avctx->height) {
94 int ret = ff_set_dimensions(avctx, 80 << 3, 25 << 4);
97 } else if (avctx->width % FONT_WIDTH || avctx->height % s->font_height) {
98 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions %d %d\n", avctx->width, avctx->height);
99 return AVERROR(EINVAL);
104 static void set_palette(uint32_t *pal)
107 memcpy(pal, ff_cga_palette, 16 * 4);
109 #define COLOR(x) ((x) * 40 + 55)
110 for (r = 0; r < 6; r++)
111 for (g = 0; g < 6; g++)
112 for (b = 0; b < 6; b++)
113 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
114 #define GRAY(x) ((x) * 10 + 8)
115 for (g = 0; g < 24; g++)
116 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
119 static void hscroll(AVCodecContext *avctx)
121 AnsiContext *s = avctx->priv_data;
124 if (s->y <= avctx->height - 2*s->font_height) {
125 s->y += s->font_height;
130 for (; i < avctx->height - s->font_height; i++)
131 memcpy(s->frame->data[0] + i * s->frame->linesize[0],
132 s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
134 for (; i < avctx->height; i++)
135 memset(s->frame->data[0] + i * s->frame->linesize[0],
136 DEFAULT_BG_COLOR, avctx->width);
139 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
141 AnsiContext *s = avctx->priv_data;
143 for (i = 0; i < s->font_height; i++)
144 memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
145 DEFAULT_BG_COLOR, xlength);
148 static void erase_screen(AVCodecContext *avctx)
150 AnsiContext *s = avctx->priv_data;
152 for (i = 0; i < avctx->height; i++)
153 memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
158 * Draw character to screen
160 static void draw_char(AVCodecContext *avctx, int c)
162 AnsiContext *s = avctx->priv_data;
166 if ((s->attributes & ATTR_BOLD))
168 if ((s->attributes & ATTR_BLINK))
170 if ((s->attributes & ATTR_REVERSE))
172 if ((s->attributes & ATTR_CONCEALED))
174 ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
175 s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
177 if (s->x > avctx->width - FONT_WIDTH) {
184 * Execute ANSI escape code
185 * @return 0 on success, negative on error
187 static int execute_code(AVCodecContext * avctx, int c)
189 AnsiContext *s = avctx->priv_data;
191 int width = avctx->width;
192 int height = avctx->height;
195 case 'A': //Cursor Up
196 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
198 case 'B': //Cursor Down
199 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
201 case 'C': //Cursor Right
202 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
204 case 'D': //Cursor Left
205 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
207 case 'H': //Cursor Position
208 case 'f': //Horizontal and Vertical Position
209 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
210 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
212 case 'h': //set screen mode
213 case 'l': //reset screen mode
215 s->args[0] = DEFAULT_SCREEN_MODE;
217 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
218 s->font = avpriv_cga_font;
223 case 2: case 3: //640x400 (25 rows)
224 s->font = avpriv_vga16_font;
229 case 6: case 14: //640x200 (25 rows)
230 s->font = avpriv_cga_font;
235 case 7: //set line wrapping
237 case 15: case 16: //640x350 (43 rows)
238 s->font = avpriv_cga_font;
243 case 17: case 18: //640x480 (60 rows)
244 s->font = avpriv_cga_font;
250 avpriv_request_sample(avctx, "Unsupported screen mode");
252 s->x = av_clip(s->x, 0, width - FONT_WIDTH);
253 s->y = av_clip(s->y, 0, height - s->font_height);
254 if (width != avctx->width || height != avctx->height) {
255 av_frame_unref(s->frame);
256 ret = ff_set_dimensions(avctx, width, height);
259 if ((ret = ff_get_buffer(avctx, s->frame,
260 AV_GET_BUFFER_FLAG_REF)) < 0)
262 s->frame->pict_type = AV_PICTURE_TYPE_I;
263 s->frame->palette_has_changed = 1;
264 set_palette((uint32_t *)s->frame->data[1]);
266 } else if (c == 'l') {
270 case 'J': //Erase in Page
271 switch (s->args[0]) {
273 erase_line(avctx, s->x, avctx->width - s->x);
274 if (s->y < avctx->height - s->font_height)
275 memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
276 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
279 erase_line(avctx, 0, s->x);
281 memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
287 case 'K': //Erase in Line
290 erase_line(avctx, s->x, avctx->width - s->x);
293 erase_line(avctx, 0, s->x);
296 erase_line(avctx, 0, avctx->width);
299 case 'm': //Select Graphics Rendition
300 if (s->nb_args == 0) {
304 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
308 s->fg = DEFAULT_FG_COLOR;
309 s->bg = DEFAULT_BG_COLOR;
310 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
311 s->attributes |= 1 << (m - 1);
312 } else if (m >= 30 && m <= 37) {
313 s->fg = ansi_to_cga[m - 30];
314 } else if (m == 38 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
315 int index = s->args[i + 2];
316 s->fg = index < 16 ? ansi_to_cga[index] : index;
318 } else if (m == 39) {
319 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
320 } else if (m >= 40 && m <= 47) {
321 s->bg = ansi_to_cga[m - 40];
322 } else if (m == 48 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
323 int index = s->args[i + 2];
324 s->bg = index < 16 ? ansi_to_cga[index] : index;
326 } else if (m == 49) {
327 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
329 avpriv_request_sample(avctx, "Unsupported rendition parameter");
333 case 'n': //Device Status Report
334 case 'R': //report current line and column
337 case 's': //Save Cursor Position
341 case 'u': //Restore Cursor Position
342 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
343 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
346 avpriv_request_sample(avctx, "Unknown escape code");
349 s->x = av_clip(s->x, 0, avctx->width - FONT_WIDTH);
350 s->y = av_clip(s->y, 0, avctx->height - s->font_height);
354 static int decode_frame(AVCodecContext *avctx,
355 void *data, int *got_frame,
358 AnsiContext *s = avctx->priv_data;
359 uint8_t *buf = avpkt->data;
360 int buf_size = avpkt->size;
361 const uint8_t *buf_end = buf+buf_size;
364 if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
366 if (!avctx->frame_number) {
367 for (i=0; i<avctx->height; i++)
368 memset(s->frame->data[0]+ i*s->frame->linesize[0], 0, avctx->width);
369 memset(s->frame->data[1], 0, AVPALETTE_SIZE);
372 s->frame->pict_type = AV_PICTURE_TYPE_I;
373 s->frame->palette_has_changed = 1;
374 set_palette((uint32_t *)s->frame->data[1]);
375 if (!s->first_frame) {
380 while(buf < buf_end) {
390 s->x = FFMAX(s->x - 1, 0);
393 i = s->x / FONT_WIDTH;
394 count = ((i + 8) & ~7) - i;
395 for (i = 0; i < count; i++)
396 draw_char(avctx, ' ');
407 s->state = STATE_ESCAPE;
410 draw_char(avctx, buf[0]);
415 s->state = STATE_CODE;
419 s->state = STATE_NORMAL;
420 draw_char(avctx, 0x1B);
426 case '0': case '1': case '2': case '3': case '4':
427 case '5': case '6': case '7': case '8': case '9':
428 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] < 6553)
429 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
433 if (s->nb_args < MAX_NB_ARGS)
434 s->args[s->nb_args] = 0;
437 s->state = STATE_MUSIC_PREAMBLE;
443 if (s->nb_args > MAX_NB_ARGS)
444 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
445 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
447 if ((ret = execute_code(avctx, buf[0])) < 0)
449 s->state = STATE_NORMAL;
452 case STATE_MUSIC_PREAMBLE:
453 if (buf[0] == 0x0E || buf[0] == 0x1B)
454 s->state = STATE_NORMAL;
455 /* ignore music data */
462 if ((ret = av_frame_ref(data, s->frame)) < 0)
467 static av_cold int decode_close(AVCodecContext *avctx)
469 AnsiContext *s = avctx->priv_data;
471 av_frame_free(&s->frame);
475 AVCodec ff_ansi_decoder = {
477 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
478 .type = AVMEDIA_TYPE_VIDEO,
479 .id = AV_CODEC_ID_ANSI,
480 .priv_data_size = sizeof(AnsiContext),
482 .close = decode_close,
483 .decode = decode_frame,
484 .capabilities = AV_CODEC_CAP_DR1,
485 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,