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"
33 #define ATTR_BOLD 0x01 /**< Bold/Bright-foreground (mode 1) */
34 #define ATTR_FAINT 0x02 /**< Faint (mode 2) */
35 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
36 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
37 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
38 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
40 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
41 #define DEFAULT_BG_COLOR 0
42 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
44 #define FONT_WIDTH 8 /**< Font width */
46 /** map ansi color index to cga palette index */
47 static const uint8_t ansi_to_cga[16] = {
48 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
53 int x; /**< x cursor position (pixels) */
54 int y; /**< y cursor position (pixels) */
55 int sx; /**< saved x cursor position (pixels) */
56 int sy; /**< saved y cursor position (pixels) */
57 const uint8_t* font; /**< font */
58 int font_height; /**< font height */
59 int attributes; /**< attribute flags */
60 int fg; /**< foreground color */
61 int bg; /**< background color */
64 /* ansi parser state machine */
72 int args[MAX_NB_ARGS];
73 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
76 static av_cold int decode_init(AVCodecContext *avctx)
78 AnsiContext *s = avctx->priv_data;
79 avctx->pix_fmt = AV_PIX_FMT_PAL8;
82 s->font = avpriv_vga16_font;
84 s->fg = DEFAULT_FG_COLOR;
85 s->bg = DEFAULT_BG_COLOR;
87 avcodec_get_frame_defaults(&s->frame);
88 if (!avctx->width || !avctx->height)
89 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
94 static void set_palette(uint32_t *pal)
97 memcpy(pal, ff_cga_palette, 16 * 4);
99 #define COLOR(x) ((x) * 40 + 55)
100 for (r = 0; r < 6; r++)
101 for (g = 0; g < 6; g++)
102 for (b = 0; b < 6; b++)
103 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
104 #define GRAY(x) ((x) * 10 + 8)
105 for (g = 0; g < 24; g++)
106 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
109 static void hscroll(AVCodecContext *avctx)
111 AnsiContext *s = avctx->priv_data;
114 if (s->y < avctx->height - s->font_height) {
115 s->y += s->font_height;
120 for (; i < avctx->height - s->font_height; i++)
121 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
122 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
124 for (; i < avctx->height; i++)
125 memset(s->frame.data[0] + i * s->frame.linesize[0],
126 DEFAULT_BG_COLOR, avctx->width);
129 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
131 AnsiContext *s = avctx->priv_data;
133 for (i = 0; i < s->font_height; i++)
134 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
135 DEFAULT_BG_COLOR, xlength);
138 static void erase_screen(AVCodecContext *avctx)
140 AnsiContext *s = avctx->priv_data;
142 for (i = 0; i < avctx->height; i++)
143 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
148 * Draw character to screen
150 static void draw_char(AVCodecContext *avctx, int c)
152 AnsiContext *s = avctx->priv_data;
156 if ((s->attributes & ATTR_BOLD))
158 if ((s->attributes & ATTR_BLINK))
160 if ((s->attributes & ATTR_REVERSE))
162 if ((s->attributes & ATTR_CONCEALED))
164 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
165 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
167 if (s->x >= avctx->width) {
174 * Execute ANSI escape code
175 * @return 0 on success, negative on error
177 static int execute_code(AVCodecContext * avctx, int c)
179 AnsiContext *s = avctx->priv_data;
180 int ret, i, width, height;
182 case 'A': //Cursor Up
183 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
185 case 'B': //Cursor Down
186 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
188 case 'C': //Cursor Right
189 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
191 case 'D': //Cursor Left
192 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
194 case 'H': //Cursor Position
195 case 'f': //Horizontal and Vertical Position
196 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
197 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
199 case 'h': //set creen mode
200 case 'l': //reset screen mode
202 s->args[0] = DEFAULT_SCREEN_MODE;
203 width = avctx->width;
204 height = avctx->height;
206 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
207 s->font = avpriv_cga_font;
212 case 2: case 3: //640x400 (25 rows)
213 s->font = avpriv_vga16_font;
218 case 6: case 14: //640x200 (25 rows)
219 s->font = avpriv_cga_font;
224 case 7: //set line wrapping
226 case 15: case 16: //640x350 (43 rows)
227 s->font = avpriv_cga_font;
232 case 17: case 18: //640x480 (60 rows)
233 s->font = avpriv_cga_font;
239 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
241 if (width != avctx->width || height != avctx->height) {
242 if (s->frame.data[0])
243 avctx->release_buffer(avctx, &s->frame);
244 avcodec_set_dimensions(avctx, width, height);
245 ret = avctx->get_buffer(avctx, &s->frame);
247 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
250 s->frame.pict_type = AV_PICTURE_TYPE_I;
251 s->frame.palette_has_changed = 1;
252 set_palette((uint32_t *)s->frame.data[1]);
254 } else if (c == 'l') {
258 case 'J': //Erase in Page
259 switch (s->args[0]) {
261 erase_line(avctx, s->x, avctx->width - s->x);
262 if (s->y < avctx->height - s->font_height)
263 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
264 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
267 erase_line(avctx, 0, s->x);
269 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
275 case 'K': //Erase in Line
278 erase_line(avctx, s->x, avctx->width - s->x);
281 erase_line(avctx, 0, s->x);
284 erase_line(avctx, 0, avctx->width);
287 case 'm': //Select Graphics Rendition
288 if (s->nb_args == 0) {
292 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
296 s->fg = DEFAULT_FG_COLOR;
297 s->bg = DEFAULT_BG_COLOR;
298 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
299 s->attributes |= 1 << (m - 1);
300 } else if (m >= 30 && m <= 37) {
301 s->fg = ansi_to_cga[m - 30];
302 } else if (m == 38 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
303 int index = s->args[i + 2];
304 s->fg = index < 16 ? ansi_to_cga[index] : index;
306 } else if (m == 39) {
307 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
308 } else if (m >= 40 && m <= 47) {
309 s->bg = ansi_to_cga[m - 40];
310 } else if (m == 48 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
311 int index = s->args[i + 2];
312 s->bg = index < 16 ? ansi_to_cga[index] : index;
314 } else if (m == 49) {
315 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
317 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
321 case 'n': //Device Status Report
322 case 'R': //report current line and column
325 case 's': //Save Cursor Position
329 case 'u': //Restore Cursor Position
330 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
331 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
334 av_log_ask_for_sample(avctx, "unsupported escape code\n");
340 static int decode_frame(AVCodecContext *avctx,
341 void *data, int *data_size,
344 AnsiContext *s = avctx->priv_data;
345 uint8_t *buf = avpkt->data;
346 int buf_size = avpkt->size;
347 const uint8_t *buf_end = buf+buf_size;
350 ret = avctx->reget_buffer(avctx, &s->frame);
352 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
355 s->frame.pict_type = AV_PICTURE_TYPE_I;
356 s->frame.palette_has_changed = 1;
357 set_palette((uint32_t *)s->frame.data[1]);
358 if (!s->first_frame) {
363 while(buf < buf_end) {
373 s->x = FFMAX(s->x - 1, 0);
376 i = s->x / FONT_WIDTH;
377 count = ((i + 8) & ~7) - i;
378 for (i = 0; i < count; i++)
379 draw_char(avctx, ' ');
390 s->state = STATE_ESCAPE;
393 draw_char(avctx, buf[0]);
398 s->state = STATE_CODE;
402 s->state = STATE_NORMAL;
403 draw_char(avctx, 0x1B);
409 case '0': case '1': case '2': case '3': case '4':
410 case '5': case '6': case '7': case '8': case '9':
411 if (s->nb_args < MAX_NB_ARGS)
412 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
416 if (s->nb_args < MAX_NB_ARGS)
417 s->args[s->nb_args] = 0;
420 s->state = STATE_MUSIC_PREAMBLE;
426 if (s->nb_args > MAX_NB_ARGS)
427 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
428 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
430 if (execute_code(avctx, buf[0]) < 0)
432 s->state = STATE_NORMAL;
435 case STATE_MUSIC_PREAMBLE:
436 if (buf[0] == 0x0E || buf[0] == 0x1B)
437 s->state = STATE_NORMAL;
438 /* ignore music data */
444 *data_size = sizeof(AVFrame);
445 *(AVFrame*)data = s->frame;
449 static av_cold int decode_close(AVCodecContext *avctx)
451 AnsiContext *s = avctx->priv_data;
452 if (s->frame.data[0])
453 avctx->release_buffer(avctx, &s->frame);
457 AVCodec ff_ansi_decoder = {
459 .type = AVMEDIA_TYPE_VIDEO,
460 .id = AV_CODEC_ID_ANSI,
461 .priv_data_size = sizeof(AnsiContext),
463 .close = decode_close,
464 .decode = decode_frame,
465 .capabilities = CODEC_CAP_DR1,
466 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),