2 * ASCII/ANSI art decoder
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
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 */
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;
81 s->frame = av_frame_alloc();
83 return AVERROR(ENOMEM);
86 s->font = ff_vga16_font;
88 s->fg = DEFAULT_FG_COLOR;
89 s->bg = DEFAULT_BG_COLOR;
91 if (!avctx->width || !avctx->height)
92 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
97 static void hscroll(AVCodecContext *avctx)
99 AnsiContext *s = avctx->priv_data;
102 if (s->y < avctx->height - s->font_height) {
103 s->y += s->font_height;
108 for (; i < avctx->height - s->font_height; i++)
109 memcpy(s->frame->data[0] + i * s->frame->linesize[0],
110 s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
112 for (; i < avctx->height; i++)
113 memset(s->frame->data[0] + i * s->frame->linesize[0],
114 DEFAULT_BG_COLOR, avctx->width);
117 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
119 AnsiContext *s = avctx->priv_data;
121 for (i = 0; i < s->font_height; i++)
122 memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
123 DEFAULT_BG_COLOR, xlength);
126 static void erase_screen(AVCodecContext *avctx)
128 AnsiContext *s = avctx->priv_data;
130 for (i = 0; i < avctx->height; i++)
131 memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
136 * Draw character to screen
138 static void draw_char(AVCodecContext *avctx, int c)
140 AnsiContext *s = avctx->priv_data;
144 if ((s->attributes & ATTR_BOLD))
146 if ((s->attributes & ATTR_BLINK))
148 if ((s->attributes & ATTR_REVERSE))
150 if ((s->attributes & ATTR_CONCEALED))
152 ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
153 s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
155 if (s->x >= avctx->width) {
162 * Execute ANSI escape code
163 * @return 0 on success, negative on error
165 static int execute_code(AVCodecContext * avctx, int c)
167 AnsiContext *s = avctx->priv_data;
168 int ret, i, width, height;
170 case 'A': //Cursor Up
171 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
173 case 'B': //Cursor Down
174 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
176 case 'C': //Cursor Right
177 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
179 case 'D': //Cursor Left
180 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
182 case 'H': //Cursor Position
183 case 'f': //Horizontal and Vertical Position
184 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
185 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
187 case 'h': //set creen mode
188 case 'l': //reset screen mode
190 s->args[0] = DEFAULT_SCREEN_MODE;
192 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
193 s->font = ff_cga_font;
198 case 2: case 3: //640x400 (25 rows)
199 s->font = ff_vga16_font;
204 case 6: case 14: //640x200 (25 rows)
205 s->font = ff_cga_font;
210 case 7: //set line wrapping
212 case 15: case 16: //640x350 (43 rows)
213 s->font = ff_cga_font;
218 case 17: case 18: //640x480 (60 rows)
219 s->font = ff_cga_font;
225 avpriv_request_sample(avctx, "Unsupported screen mode");
227 if (width != avctx->width || height != avctx->height) {
228 av_frame_unref(s->frame);
229 avcodec_set_dimensions(avctx, width, height);
230 ret = ff_get_buffer(avctx, s->frame, AV_GET_BUFFER_FLAG_REF);
232 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
235 s->frame->pict_type = AV_PICTURE_TYPE_I;
236 s->frame->palette_has_changed = 1;
237 memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
239 } else if (c == 'l') {
243 case 'J': //Erase in Page
244 switch (s->args[0]) {
246 erase_line(avctx, s->x, avctx->width - s->x);
247 if (s->y < avctx->height - s->font_height)
248 memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
249 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
252 erase_line(avctx, 0, s->x);
254 memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
260 case 'K': //Erase in Line
263 erase_line(avctx, s->x, avctx->width - s->x);
266 erase_line(avctx, 0, s->x);
269 erase_line(avctx, 0, avctx->width);
272 case 'm': //Select Graphics Rendition
273 if (s->nb_args == 0) {
277 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
281 s->fg = DEFAULT_FG_COLOR;
282 s->bg = DEFAULT_BG_COLOR;
283 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
284 s->attributes |= 1 << (m - 1);
285 } else if (m >= 30 && m <= 38) {
286 s->fg = ansi_to_cga[m - 30];
287 } else if (m == 39) {
288 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
289 } else if (m >= 40 && m <= 47) {
290 s->bg = ansi_to_cga[m - 40];
291 } else if (m == 49) {
292 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
294 avpriv_request_sample(avctx, "Unsupported rendition parameter");
298 case 'n': //Device Status Report
299 case 'R': //report current line and column
302 case 's': //Save Cursor Position
306 case 'u': //Restore Cursor Position
307 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
308 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
311 avpriv_request_sample(avctx, "Unknown escape code");
317 static int decode_frame(AVCodecContext *avctx,
318 void *data, int *got_frame,
321 AnsiContext *s = avctx->priv_data;
322 uint8_t *buf = avpkt->data;
323 int buf_size = avpkt->size;
324 const uint8_t *buf_end = buf+buf_size;
327 ret = ff_reget_buffer(avctx, s->frame);
329 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
332 if (!avctx->frame_number) {
333 memset(s->frame->data[0], 0, avctx->height * FFABS(s->frame->linesize[0]));
334 memset(s->frame->data[1], 0, AVPALETTE_SIZE);
337 s->frame->pict_type = AV_PICTURE_TYPE_I;
338 s->frame->palette_has_changed = 1;
339 memcpy(s->frame->data[1], ff_cga_palette, 16 * 4);
341 while(buf < buf_end) {
351 s->x = FFMAX(s->x - 1, 0);
354 i = s->x / FONT_WIDTH;
355 count = ((i + 8) & ~7) - i;
356 for (i = 0; i < count; i++)
357 draw_char(avctx, ' ');
368 s->state = STATE_ESCAPE;
371 draw_char(avctx, buf[0]);
376 s->state = STATE_CODE;
380 s->state = STATE_NORMAL;
381 draw_char(avctx, 0x1B);
387 case '0': case '1': case '2': case '3': case '4':
388 case '5': case '6': case '7': case '8': case '9':
389 if (s->nb_args < MAX_NB_ARGS)
390 s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
394 if (s->nb_args < MAX_NB_ARGS)
395 s->args[s->nb_args] = 0;
398 s->state = STATE_MUSIC_PREAMBLE;
404 if (s->nb_args > MAX_NB_ARGS)
405 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
406 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
408 if ((ret = execute_code(avctx, buf[0])) < 0)
410 s->state = STATE_NORMAL;
413 case STATE_MUSIC_PREAMBLE:
414 if (buf[0] == 0x0E || buf[0] == 0x1B)
415 s->state = STATE_NORMAL;
416 /* ignore music data */
423 if ((ret = av_frame_ref(data, s->frame)) < 0)
428 static av_cold int decode_close(AVCodecContext *avctx)
430 AnsiContext *s = avctx->priv_data;
432 av_frame_free(&s->frame);
436 AVCodec ff_ansi_decoder = {
438 .type = AVMEDIA_TYPE_VIDEO,
439 .id = AV_CODEC_ID_ANSI,
440 .priv_data_size = sizeof(AnsiContext),
442 .close = decode_close,
443 .decode = decode_frame,
444 .capabilities = CODEC_CAP_DR1,
445 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),