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
52 typedef struct AnsiContext
{
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 int ret
= ff_set_dimensions(avctx
, 80 << 3, 25 << 4);
99 static void hscroll(AVCodecContext
*avctx
)
101 AnsiContext
*s
= avctx
->priv_data
;
104 if (s
->y
< avctx
->height
- s
->font_height
) {
105 s
->y
+= s
->font_height
;
110 for (; i
< avctx
->height
- s
->font_height
; i
++)
111 memcpy(s
->frame
->data
[0] + i
* s
->frame
->linesize
[0],
112 s
->frame
->data
[0] + (i
+ s
->font_height
) * s
->frame
->linesize
[0],
114 for (; i
< avctx
->height
; i
++)
115 memset(s
->frame
->data
[0] + i
* s
->frame
->linesize
[0],
116 DEFAULT_BG_COLOR
, avctx
->width
);
119 static void erase_line(AVCodecContext
* avctx
, int xoffset
, int xlength
)
121 AnsiContext
*s
= avctx
->priv_data
;
123 for (i
= 0; i
< s
->font_height
; i
++)
124 memset(s
->frame
->data
[0] + (s
->y
+ i
)*s
->frame
->linesize
[0] + xoffset
,
125 DEFAULT_BG_COLOR
, xlength
);
128 static void erase_screen(AVCodecContext
*avctx
)
130 AnsiContext
*s
= avctx
->priv_data
;
132 for (i
= 0; i
< avctx
->height
; i
++)
133 memset(s
->frame
->data
[0] + i
* s
->frame
->linesize
[0], DEFAULT_BG_COLOR
, avctx
->width
);
138 * Draw character to screen
140 static void draw_char(AVCodecContext
*avctx
, int c
)
142 AnsiContext
*s
= avctx
->priv_data
;
146 if ((s
->attributes
& ATTR_BOLD
))
148 if ((s
->attributes
& ATTR_BLINK
))
150 if ((s
->attributes
& ATTR_REVERSE
))
152 if ((s
->attributes
& ATTR_CONCEALED
))
154 ff_draw_pc_font(s
->frame
->data
[0] + s
->y
* s
->frame
->linesize
[0] + s
->x
,
155 s
->frame
->linesize
[0], s
->font
, s
->font_height
, c
, fg
, bg
);
157 if (s
->x
>= avctx
->width
) {
164 * Execute ANSI escape code
165 * @return 0 on success, negative on error
167 static int execute_code(AVCodecContext
* avctx
, int c
)
169 AnsiContext
*s
= avctx
->priv_data
;
175 case 'A': //Cursor Up
176 s
->y
= FFMAX(s
->y
- (s
->nb_args
> 0 ? s
->args
[0]*s
->font_height
: s
->font_height
), 0);
178 case 'B': //Cursor Down
179 s
->y
= FFMIN(s
->y
+ (s
->nb_args
> 0 ? s
->args
[0]*s
->font_height
: s
->font_height
), avctx
->height
- s
->font_height
);
181 case 'C': //Cursor Right
182 s
->x
= FFMIN(s
->x
+ (s
->nb_args
> 0 ? s
->args
[0]*FONT_WIDTH
: FONT_WIDTH
), avctx
->width
- FONT_WIDTH
);
184 case 'D': //Cursor Left
185 s
->x
= FFMAX(s
->x
- (s
->nb_args
> 0 ? s
->args
[0]*FONT_WIDTH
: FONT_WIDTH
), 0);
187 case 'H': //Cursor Position
188 case 'f': //Horizontal and Vertical Position
189 s
->y
= s
->nb_args
> 0 ? av_clip((s
->args
[0] - 1)*s
->font_height
, 0, avctx
->height
- s
->font_height
) : 0;
190 s
->x
= s
->nb_args
> 1 ? av_clip((s
->args
[1] - 1)*FONT_WIDTH
, 0, avctx
->width
- FONT_WIDTH
) : 0;
192 case 'h': //set screen mode
193 case 'l': //reset screen mode
195 s
->args
[0] = DEFAULT_SCREEN_MODE
;
197 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
198 s
->font
= ff_cga_font
;
203 case 2: case 3: //640x400 (25 rows)
204 s
->font
= ff_vga16_font
;
209 case 6: case 14: //640x200 (25 rows)
210 s
->font
= ff_cga_font
;
215 case 7: //set line wrapping
217 case 15: case 16: //640x350 (43 rows)
218 s
->font
= ff_cga_font
;
223 case 17: case 18: //640x480 (60 rows)
224 s
->font
= ff_cga_font
;
230 avpriv_request_sample(avctx
, "Unsupported screen mode");
232 if (width
!= 0 && height
!= 0 &&
233 (width
!= avctx
->width
|| height
!= avctx
->height
)) {
234 av_frame_unref(s
->frame
);
235 ret
= ff_set_dimensions(avctx
, width
, height
);
238 ret
= ff_get_buffer(avctx
, s
->frame
, AV_GET_BUFFER_FLAG_REF
);
240 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
243 s
->frame
->pict_type
= AV_PICTURE_TYPE_I
;
244 s
->frame
->palette_has_changed
= 1;
245 memcpy(s
->frame
->data
[1], ff_cga_palette
, 16 * 4);
247 } else if (c
== 'l') {
251 case 'J': //Erase in Page
252 switch (s
->args
[0]) {
254 erase_line(avctx
, s
->x
, avctx
->width
- s
->x
);
255 if (s
->y
< avctx
->height
- s
->font_height
)
256 memset(s
->frame
->data
[0] + (s
->y
+ s
->font_height
)*s
->frame
->linesize
[0],
257 DEFAULT_BG_COLOR
, (avctx
->height
- s
->y
- s
->font_height
)*s
->frame
->linesize
[0]);
260 erase_line(avctx
, 0, s
->x
);
262 memset(s
->frame
->data
[0], DEFAULT_BG_COLOR
, s
->y
* s
->frame
->linesize
[0]);
268 case 'K': //Erase in Line
271 erase_line(avctx
, s
->x
, avctx
->width
- s
->x
);
274 erase_line(avctx
, 0, s
->x
);
277 erase_line(avctx
, 0, avctx
->width
);
280 case 'm': //Select Graphics Rendition
281 if (s
->nb_args
== 0) {
285 for (i
= 0; i
< FFMIN(s
->nb_args
, MAX_NB_ARGS
); i
++) {
289 s
->fg
= DEFAULT_FG_COLOR
;
290 s
->bg
= DEFAULT_BG_COLOR
;
291 } else if (m
== 1 || m
== 2 || m
== 4 || m
== 5 || m
== 7 || m
== 8) {
292 s
->attributes
|= 1 << (m
- 1);
293 } else if (m
>= 30 && m
<= 38) {
294 s
->fg
= ansi_to_cga
[m
- 30];
295 } else if (m
== 39) {
296 s
->fg
= ansi_to_cga
[DEFAULT_FG_COLOR
];
297 } else if (m
>= 40 && m
<= 47) {
298 s
->bg
= ansi_to_cga
[m
- 40];
299 } else if (m
== 49) {
300 s
->fg
= ansi_to_cga
[DEFAULT_BG_COLOR
];
302 avpriv_request_sample(avctx
, "Unsupported rendition parameter");
306 case 'n': //Device Status Report
307 case 'R': //report current line and column
310 case 's': //Save Cursor Position
314 case 'u': //Restore Cursor Position
315 s
->x
= av_clip(s
->sx
, 0, avctx
->width
- FONT_WIDTH
);
316 s
->y
= av_clip(s
->sy
, 0, avctx
->height
- s
->font_height
);
319 avpriv_request_sample(avctx
, "Unknown escape code");
325 static int decode_frame(AVCodecContext
*avctx
,
326 void *data
, int *got_frame
,
329 AnsiContext
*s
= avctx
->priv_data
;
330 uint8_t *buf
= avpkt
->data
;
331 int buf_size
= avpkt
->size
;
332 const uint8_t *buf_end
= buf
+buf_size
;
335 ret
= ff_reget_buffer(avctx
, s
->frame
);
337 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
340 if (!avctx
->frame_number
) {
341 memset(s
->frame
->data
[0], 0, avctx
->height
* FFABS(s
->frame
->linesize
[0]));
342 memset(s
->frame
->data
[1], 0, AVPALETTE_SIZE
);
345 s
->frame
->pict_type
= AV_PICTURE_TYPE_I
;
346 s
->frame
->palette_has_changed
= 1;
347 memcpy(s
->frame
->data
[1], ff_cga_palette
, 16 * 4);
349 while(buf
< buf_end
) {
359 s
->x
= FFMAX(s
->x
- 1, 0);
362 i
= s
->x
/ FONT_WIDTH
;
363 count
= ((i
+ 8) & ~7) - i
;
364 for (i
= 0; i
< count
; i
++)
365 draw_char(avctx
, ' ');
376 s
->state
= STATE_ESCAPE
;
379 draw_char(avctx
, buf
[0]);
384 s
->state
= STATE_CODE
;
388 s
->state
= STATE_NORMAL
;
389 draw_char(avctx
, 0x1B);
395 case '0': case '1': case '2': case '3': case '4':
396 case '5': case '6': case '7': case '8': case '9':
397 if (s
->nb_args
< MAX_NB_ARGS
)
398 s
->args
[s
->nb_args
] = s
->args
[s
->nb_args
] * 10 + buf
[0] - '0';
402 if (s
->nb_args
< MAX_NB_ARGS
)
403 s
->args
[s
->nb_args
] = 0;
406 s
->state
= STATE_MUSIC_PREAMBLE
;
412 if (s
->nb_args
> MAX_NB_ARGS
)
413 av_log(avctx
, AV_LOG_WARNING
, "args overflow (%i)\n", s
->nb_args
);
414 if (s
->nb_args
< MAX_NB_ARGS
&& s
->args
[s
->nb_args
])
416 if ((ret
= execute_code(avctx
, buf
[0])) < 0)
418 s
->state
= STATE_NORMAL
;
421 case STATE_MUSIC_PREAMBLE
:
422 if (buf
[0] == 0x0E || buf
[0] == 0x1B)
423 s
->state
= STATE_NORMAL
;
424 /* ignore music data */
431 if ((ret
= av_frame_ref(data
, s
->frame
)) < 0)
436 static av_cold
int decode_close(AVCodecContext
*avctx
)
438 AnsiContext
*s
= avctx
->priv_data
;
440 av_frame_free(&s
->frame
);
444 AVCodec ff_ansi_decoder
= {
446 .long_name
= NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
447 .type
= AVMEDIA_TYPE_VIDEO
,
448 .id
= AV_CODEC_ID_ANSI
,
449 .priv_data_size
= sizeof(AnsiContext
),
451 .close
= decode_close
,
452 .decode
= decode_frame
,
453 .capabilities
= AV_CODEC_CAP_DR1
,
454 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE
,