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_ITALICS 0x04 /**< Italics (mode 3) */
38 #define ATTR_UNDERLINE 0x08 /**< Underline (mode 4) */
39 #define ATTR_BLINK 0x10 /**< Blink/Bright-background (mode 5) */
40 #define ATTR_REVERSE 0x40 /**< Reverse (mode 7) */
41 #define ATTR_CONCEALED 0x80 /**< Concealed (mode 8) */
43 #define DEFAULT_FG_COLOR 7 /**< CGA color index */
44 #define DEFAULT_BG_COLOR 0
45 #define DEFAULT_SCREEN_MODE 3 /**< 80x25 */
47 #define FONT_WIDTH 8 /**< Font width */
49 /** map ansi color index to cga palette index */
50 static const uint8_t ansi_to_cga[16] = {
51 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
54 typedef struct AnsiContext {
56 int x; /**< x cursor position (pixels) */
57 int y; /**< y cursor position (pixels) */
58 int sx; /**< saved x cursor position (pixels) */
59 int sy; /**< saved y cursor position (pixels) */
60 const uint8_t* font; /**< font */
61 int font_height; /**< font height */
62 int attributes; /**< attribute flags */
63 int fg; /**< foreground color */
64 int bg; /**< background color */
67 /* ansi parser state machine */
75 int args[MAX_NB_ARGS];
76 int nb_args; /**< number of arguments (may exceed MAX_NB_ARGS) */
79 static av_cold int decode_init(AVCodecContext *avctx)
81 AnsiContext *s = avctx->priv_data;
82 avctx->pix_fmt = AV_PIX_FMT_PAL8;
85 s->font = avpriv_vga16_font;
87 s->fg = DEFAULT_FG_COLOR;
88 s->bg = DEFAULT_BG_COLOR;
90 if (!avctx->width || !avctx->height) {
91 int ret = ff_set_dimensions(avctx, 80 << 3, 25 << 4);
94 } else if (avctx->width % FONT_WIDTH || avctx->height % s->font_height) {
95 av_log(avctx, AV_LOG_ERROR, "Invalid dimensions %d %d\n", avctx->width, avctx->height);
96 return AVERROR(EINVAL);
99 s->frame = av_frame_alloc();
101 return AVERROR(ENOMEM);
106 static void set_palette(uint32_t *pal)
109 memcpy(pal, ff_cga_palette, 16 * 4);
111 #define COLOR(x) ((x) * 40 + 55)
112 for (r = 0; r < 6; r++)
113 for (g = 0; g < 6; g++)
114 for (b = 0; b < 6; b++)
115 *pal++ = 0xFF000000 | (COLOR(r) << 16) | (COLOR(g) << 8) | COLOR(b);
116 #define GRAY(x) ((x) * 10 + 8)
117 for (g = 0; g < 24; g++)
118 *pal++ = 0xFF000000 | (GRAY(g) << 16) | (GRAY(g) << 8) | GRAY(g);
121 static void hscroll(AVCodecContext *avctx)
123 AnsiContext *s = avctx->priv_data;
126 if (s->y <= avctx->height - 2*s->font_height) {
127 s->y += s->font_height;
132 for (; i < avctx->height - s->font_height; i++)
133 memcpy(s->frame->data[0] + i * s->frame->linesize[0],
134 s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
136 for (; i < avctx->height; i++)
137 memset(s->frame->data[0] + i * s->frame->linesize[0],
138 DEFAULT_BG_COLOR, avctx->width);
141 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
143 AnsiContext *s = avctx->priv_data;
145 for (i = 0; i < s->font_height; i++)
146 memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
147 DEFAULT_BG_COLOR, xlength);
150 static void erase_screen(AVCodecContext *avctx)
152 AnsiContext *s = avctx->priv_data;
154 for (i = 0; i < avctx->height; i++)
155 memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
160 * Draw character to screen
162 static void draw_char(AVCodecContext *avctx, int c)
164 AnsiContext *s = avctx->priv_data;
168 if ((s->attributes & ATTR_BOLD))
170 if ((s->attributes & ATTR_BLINK))
172 if ((s->attributes & ATTR_REVERSE))
174 if ((s->attributes & ATTR_CONCEALED))
176 ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
177 s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
179 if (s->x > avctx->width - FONT_WIDTH) {
186 * Execute ANSI escape code
187 * @return 0 on success, negative on error
189 static int execute_code(AVCodecContext * avctx, int c)
191 AnsiContext *s = avctx->priv_data;
193 int width = avctx->width;
194 int height = avctx->height;
197 case 'A': //Cursor Up
198 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
200 case 'B': //Cursor Down
201 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
203 case 'C': //Cursor Right
204 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
206 case 'D': //Cursor Left
207 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
209 case 'H': //Cursor Position
210 case 'f': //Horizontal and Vertical Position
211 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
212 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
214 case 'h': //set screen mode
215 case 'l': //reset screen mode
217 s->args[0] = DEFAULT_SCREEN_MODE;
219 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
220 s->font = avpriv_cga_font;
225 case 2: case 3: //640x400 (25 rows)
226 s->font = avpriv_vga16_font;
231 case 6: case 14: //640x200 (25 rows)
232 s->font = avpriv_cga_font;
237 case 7: //set line wrapping
239 case 15: case 16: //640x350 (43 rows)
240 s->font = avpriv_cga_font;
245 case 17: case 18: //640x480 (60 rows)
246 s->font = avpriv_cga_font;
252 avpriv_request_sample(avctx, "Unsupported screen mode");
254 s->x = av_clip(s->x, 0, width - FONT_WIDTH);
255 s->y = av_clip(s->y, 0, height - s->font_height);
256 if (width != avctx->width || height != avctx->height) {
257 av_frame_unref(s->frame);
258 ret = ff_set_dimensions(avctx, width, height);
261 if ((ret = ff_get_buffer(avctx, s->frame,
262 AV_GET_BUFFER_FLAG_REF)) < 0)
264 s->frame->pict_type = AV_PICTURE_TYPE_I;
265 s->frame->palette_has_changed = 1;
266 set_palette((uint32_t *)s->frame->data[1]);
268 } else if (c == 'l') {
272 case 'J': //Erase in Page
273 switch (s->args[0]) {
275 erase_line(avctx, s->x, avctx->width - s->x);
276 if (s->y < avctx->height - s->font_height)
277 memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
278 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
281 erase_line(avctx, 0, s->x);
283 memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
289 case 'K': //Erase in Line
292 erase_line(avctx, s->x, avctx->width - s->x);
295 erase_line(avctx, 0, s->x);
298 erase_line(avctx, 0, avctx->width);
301 case 'm': //Select Graphics Rendition
302 if (s->nb_args == 0) {
306 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
310 s->fg = DEFAULT_FG_COLOR;
311 s->bg = DEFAULT_BG_COLOR;
312 } else if (m == 1 || m == 2 || m == 3 || m == 4 || m == 5 || m == 7 || m == 8) {
313 s->attributes |= 1 << (m - 1);
314 } else if (m >= 30 && m <= 37) {
315 s->fg = ansi_to_cga[m - 30];
316 } else if (m == 38 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
317 int index = s->args[i + 2];
318 s->fg = index < 16 ? ansi_to_cga[index] : index;
320 } else if (m == 39) {
321 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
322 } else if (m >= 40 && m <= 47) {
323 s->bg = ansi_to_cga[m - 40];
324 } else if (m == 48 && i + 2 < FFMIN(s->nb_args, MAX_NB_ARGS) && s->args[i + 1] == 5 && s->args[i + 2] < 256) {
325 int index = s->args[i + 2];
326 s->bg = index < 16 ? ansi_to_cga[index] : index;
328 } else if (m == 49) {
329 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
331 avpriv_request_sample(avctx, "Unsupported rendition parameter");
335 case 'n': //Device Status Report
336 case 'R': //report current line and column
339 case 's': //Save Cursor Position
343 case 'u': //Restore Cursor Position
344 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
345 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
348 avpriv_request_sample(avctx, "Unknown escape code");
351 s->x = av_clip(s->x, 0, avctx->width - FONT_WIDTH);
352 s->y = av_clip(s->y, 0, avctx->height - s->font_height);
356 static int decode_frame(AVCodecContext *avctx,
357 void *data, int *got_frame,
360 AnsiContext *s = avctx->priv_data;
361 uint8_t *buf = avpkt->data;
362 int buf_size = avpkt->size;
363 const uint8_t *buf_end = buf+buf_size;
366 if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0)
368 if (!avctx->frame_number) {
369 for (i=0; i<avctx->height; i++)
370 memset(s->frame->data[0]+ i*s->frame->linesize[0], 0, avctx->width);
371 memset(s->frame->data[1], 0, AVPALETTE_SIZE);
374 s->frame->pict_type = AV_PICTURE_TYPE_I;
375 s->frame->palette_has_changed = 1;
376 set_palette((uint32_t *)s->frame->data[1]);
377 if (!s->first_frame) {
382 while(buf < buf_end) {
392 s->x = FFMAX(s->x - 1, 0);
395 i = s->x / FONT_WIDTH;
396 count = ((i + 8) & ~7) - i;
397 for (i = 0; i < count; i++)
398 draw_char(avctx, ' ');
409 s->state = STATE_ESCAPE;
412 draw_char(avctx, buf[0]);
417 s->state = STATE_CODE;
421 s->state = STATE_NORMAL;
422 draw_char(avctx, 0x1B);
428 case '0': case '1': case '2': case '3': case '4':
429 case '5': case '6': case '7': case '8': case '9':
430 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] < 6553)
431 s->args[s->nb_args] = FFMAX(s->args[s->nb_args], 0) * 10 + buf[0] - '0';
434 if (s->nb_args < MAX_NB_ARGS)
436 if (s->nb_args < MAX_NB_ARGS)
437 s->args[s->nb_args] = 0;
440 s->state = STATE_MUSIC_PREAMBLE;
446 if (s->nb_args > MAX_NB_ARGS)
447 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
448 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args] >= 0)
450 if ((ret = execute_code(avctx, buf[0])) < 0)
452 s->state = STATE_NORMAL;
455 case STATE_MUSIC_PREAMBLE:
456 if (buf[0] == 0x0E || buf[0] == 0x1B)
457 s->state = STATE_NORMAL;
458 /* ignore music data */
465 if ((ret = av_frame_ref(data, s->frame)) < 0)
470 static av_cold int decode_close(AVCodecContext *avctx)
472 AnsiContext *s = avctx->priv_data;
474 av_frame_free(&s->frame);
478 AVCodec ff_ansi_decoder = {
480 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
481 .type = AVMEDIA_TYPE_VIDEO,
482 .id = AV_CODEC_ID_ANSI,
483 .priv_data_size = sizeof(AnsiContext),
485 .close = decode_close,
486 .decode = decode_frame,
487 .capabilities = AV_CODEC_CAP_DR1,
488 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,