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/lfg.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 */
63 /* ansi parser state machine */
71 int args
[MAX_NB_ARGS
];
72 int nb_args
; /**< number of arguments (may exceed MAX_NB_ARGS) */
75 static av_cold
int decode_init(AVCodecContext
*avctx
)
77 AnsiContext
*s
= avctx
->priv_data
;
78 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
81 s
->font
= ff_vga16_font
;
83 s
->fg
= DEFAULT_FG_COLOR
;
84 s
->bg
= DEFAULT_BG_COLOR
;
86 if (!avctx
->width
|| !avctx
->height
)
87 avcodec_set_dimensions(avctx
, 80<<3, 25<<4);
92 static void hscroll(AVCodecContext
*avctx
)
94 AnsiContext
*s
= avctx
->priv_data
;
97 if (s
->y
< avctx
->height
- s
->font_height
) {
98 s
->y
+= s
->font_height
;
103 for (; i
< avctx
->height
- s
->font_height
; i
++)
104 memcpy(s
->frame
.data
[0] + i
* s
->frame
.linesize
[0],
105 s
->frame
.data
[0] + (i
+ s
->font_height
) * s
->frame
.linesize
[0],
107 for (; i
< avctx
->height
; i
++)
108 memset(s
->frame
.data
[0] + i
* s
->frame
.linesize
[0],
109 DEFAULT_BG_COLOR
, avctx
->width
);
112 static void erase_line(AVCodecContext
* avctx
, int xoffset
, int xlength
)
114 AnsiContext
*s
= avctx
->priv_data
;
116 for (i
= 0; i
< s
->font_height
; i
++)
117 memset(s
->frame
.data
[0] + (s
->y
+ i
)*s
->frame
.linesize
[0] + xoffset
,
118 DEFAULT_BG_COLOR
, xlength
);
121 static void erase_screen(AVCodecContext
*avctx
)
123 AnsiContext
*s
= avctx
->priv_data
;
125 for (i
= 0; i
< avctx
->height
; i
++)
126 memset(s
->frame
.data
[0] + i
* s
->frame
.linesize
[0], DEFAULT_BG_COLOR
, avctx
->width
);
131 * Draw character to screen
133 static void draw_char(AVCodecContext
*avctx
, int c
)
135 AnsiContext
*s
= avctx
->priv_data
;
139 if ((s
->attributes
& ATTR_BOLD
))
141 if ((s
->attributes
& ATTR_BLINK
))
143 if ((s
->attributes
& ATTR_REVERSE
))
145 if ((s
->attributes
& ATTR_CONCEALED
))
147 ff_draw_pc_font(s
->frame
.data
[0] + s
->y
* s
->frame
.linesize
[0] + s
->x
,
148 s
->frame
.linesize
[0], s
->font
, s
->font_height
, c
, fg
, bg
);
150 if (s
->x
>= avctx
->width
) {
157 * Execute ANSI escape code
158 * @return 0 on success, negative on error
160 static int execute_code(AVCodecContext
* avctx
, int c
)
162 AnsiContext
*s
= avctx
->priv_data
;
163 int ret
, i
, width
, height
;
165 case 'A': //Cursor Up
166 s
->y
= FFMAX(s
->y
- (s
->nb_args
> 0 ? s
->args
[0]*s
->font_height
: s
->font_height
), 0);
168 case 'B': //Cursor Down
169 s
->y
= FFMIN(s
->y
+ (s
->nb_args
> 0 ? s
->args
[0]*s
->font_height
: s
->font_height
), avctx
->height
- s
->font_height
);
171 case 'C': //Cursor Right
172 s
->x
= FFMIN(s
->x
+ (s
->nb_args
> 0 ? s
->args
[0]*FONT_WIDTH
: FONT_WIDTH
), avctx
->width
- FONT_WIDTH
);
174 case 'D': //Cursor Left
175 s
->x
= FFMAX(s
->x
- (s
->nb_args
> 0 ? s
->args
[0]*FONT_WIDTH
: FONT_WIDTH
), 0);
177 case 'H': //Cursor Position
178 case 'f': //Horizontal and Vertical Position
179 s
->y
= s
->nb_args
> 0 ? av_clip((s
->args
[0] - 1)*s
->font_height
, 0, avctx
->height
- s
->font_height
) : 0;
180 s
->x
= s
->nb_args
> 1 ? av_clip((s
->args
[1] - 1)*FONT_WIDTH
, 0, avctx
->width
- FONT_WIDTH
) : 0;
182 case 'h': //set creen mode
183 case 'l': //reset screen mode
185 s
->args
[0] = DEFAULT_SCREEN_MODE
;
187 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
188 s
->font
= ff_cga_font
;
193 case 2: case 3: //640x400 (25 rows)
194 s
->font
= ff_vga16_font
;
199 case 6: case 14: //640x200 (25 rows)
200 s
->font
= ff_cga_font
;
205 case 7: //set line wrapping
207 case 15: case 16: //640x350 (43 rows)
208 s
->font
= ff_cga_font
;
213 case 17: case 18: //640x480 (60 rows)
214 s
->font
= ff_cga_font
;
220 av_log_ask_for_sample(avctx
, "unsupported screen mode\n");
222 if (width
!= avctx
->width
|| height
!= avctx
->height
) {
223 if (s
->frame
.data
[0])
224 avctx
->release_buffer(avctx
, &s
->frame
);
225 avcodec_set_dimensions(avctx
, width
, height
);
226 ret
= ff_get_buffer(avctx
, &s
->frame
);
228 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
231 s
->frame
.pict_type
= AV_PICTURE_TYPE_I
;
232 s
->frame
.palette_has_changed
= 1;
233 memcpy(s
->frame
.data
[1], ff_cga_palette
, 16 * 4);
235 } else if (c
== 'l') {
239 case 'J': //Erase in Page
240 switch (s
->args
[0]) {
242 erase_line(avctx
, s
->x
, avctx
->width
- s
->x
);
243 if (s
->y
< avctx
->height
- s
->font_height
)
244 memset(s
->frame
.data
[0] + (s
->y
+ s
->font_height
)*s
->frame
.linesize
[0],
245 DEFAULT_BG_COLOR
, (avctx
->height
- s
->y
- s
->font_height
)*s
->frame
.linesize
[0]);
248 erase_line(avctx
, 0, s
->x
);
250 memset(s
->frame
.data
[0], DEFAULT_BG_COLOR
, s
->y
* s
->frame
.linesize
[0]);
256 case 'K': //Erase in Line
259 erase_line(avctx
, s
->x
, avctx
->width
- s
->x
);
262 erase_line(avctx
, 0, s
->x
);
265 erase_line(avctx
, 0, avctx
->width
);
268 case 'm': //Select Graphics Rendition
269 if (s
->nb_args
== 0) {
273 for (i
= 0; i
< FFMIN(s
->nb_args
, MAX_NB_ARGS
); i
++) {
277 s
->fg
= DEFAULT_FG_COLOR
;
278 s
->bg
= DEFAULT_BG_COLOR
;
279 } else if (m
== 1 || m
== 2 || m
== 4 || m
== 5 || m
== 7 || m
== 8) {
280 s
->attributes
|= 1 << (m
- 1);
281 } else if (m
>= 30 && m
<= 38) {
282 s
->fg
= ansi_to_cga
[m
- 30];
283 } else if (m
== 39) {
284 s
->fg
= ansi_to_cga
[DEFAULT_FG_COLOR
];
285 } else if (m
>= 40 && m
<= 47) {
286 s
->bg
= ansi_to_cga
[m
- 40];
287 } else if (m
== 49) {
288 s
->fg
= ansi_to_cga
[DEFAULT_BG_COLOR
];
290 av_log_ask_for_sample(avctx
, "unsupported rendition parameter\n");
294 case 'n': //Device Status Report
295 case 'R': //report current line and column
298 case 's': //Save Cursor Position
302 case 'u': //Restore Cursor Position
303 s
->x
= av_clip(s
->sx
, 0, avctx
->width
- FONT_WIDTH
);
304 s
->y
= av_clip(s
->sy
, 0, avctx
->height
- s
->font_height
);
307 av_log_ask_for_sample(avctx
, "unsupported escape code\n");
313 static int decode_frame(AVCodecContext
*avctx
,
314 void *data
, int *got_frame
,
317 AnsiContext
*s
= avctx
->priv_data
;
318 uint8_t *buf
= avpkt
->data
;
319 int buf_size
= avpkt
->size
;
320 const uint8_t *buf_end
= buf
+buf_size
;
323 ret
= avctx
->reget_buffer(avctx
, &s
->frame
);
325 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
328 if (!avctx
->frame_number
) {
329 memset(s
->frame
.data
[0], 0, avctx
->height
* FFABS(s
->frame
.linesize
[0]));
330 memset(s
->frame
.data
[1], 0, AVPALETTE_SIZE
);
333 s
->frame
.pict_type
= AV_PICTURE_TYPE_I
;
334 s
->frame
.palette_has_changed
= 1;
335 memcpy(s
->frame
.data
[1], ff_cga_palette
, 16 * 4);
337 while(buf
< buf_end
) {
347 s
->x
= FFMAX(s
->x
- 1, 0);
350 i
= s
->x
/ FONT_WIDTH
;
351 count
= ((i
+ 8) & ~7) - i
;
352 for (i
= 0; i
< count
; i
++)
353 draw_char(avctx
, ' ');
364 s
->state
= STATE_ESCAPE
;
367 draw_char(avctx
, buf
[0]);
372 s
->state
= STATE_CODE
;
376 s
->state
= STATE_NORMAL
;
377 draw_char(avctx
, 0x1B);
383 case '0': case '1': case '2': case '3': case '4':
384 case '5': case '6': case '7': case '8': case '9':
385 if (s
->nb_args
< MAX_NB_ARGS
)
386 s
->args
[s
->nb_args
] = s
->args
[s
->nb_args
] * 10 + buf
[0] - '0';
390 if (s
->nb_args
< MAX_NB_ARGS
)
391 s
->args
[s
->nb_args
] = 0;
394 s
->state
= STATE_MUSIC_PREAMBLE
;
400 if (s
->nb_args
> MAX_NB_ARGS
)
401 av_log(avctx
, AV_LOG_WARNING
, "args overflow (%i)\n", s
->nb_args
);
402 if (s
->nb_args
< MAX_NB_ARGS
&& s
->args
[s
->nb_args
])
404 if ((ret
= execute_code(avctx
, buf
[0])) < 0)
406 s
->state
= STATE_NORMAL
;
409 case STATE_MUSIC_PREAMBLE
:
410 if (buf
[0] == 0x0E || buf
[0] == 0x1B)
411 s
->state
= STATE_NORMAL
;
412 /* ignore music data */
419 *(AVFrame
*)data
= s
->frame
;
423 static av_cold
int decode_close(AVCodecContext
*avctx
)
425 AnsiContext
*s
= avctx
->priv_data
;
426 if (s
->frame
.data
[0])
427 avctx
->release_buffer(avctx
, &s
->frame
);
431 AVCodec ff_ansi_decoder
= {
433 .type
= AVMEDIA_TYPE_VIDEO
,
434 .id
= AV_CODEC_ID_ANSI
,
435 .priv_data_size
= sizeof(AnsiContext
),
437 .close
= decode_close
,
438 .decode
= decode_frame
,
439 .capabilities
= CODEC_CAP_DR1
,
440 .long_name
= NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),