comparison ansi.c @ 12186:fb2f04f70afe libavcodec

ASCII/ANSI art decoder
author pross
date Sun, 18 Jul 2010 08:03:35 +0000
parents
children a08f20066719
comparison
equal deleted inserted replaced
12185:a5ebc95870a2 12186:fb2f04f70afe
1 /*
2 * ASCII/ANSI art decoder
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * ASCII/ANSI art decoder
25 */
26
27 #include "avcodec.h"
28 #include "cga_data.h"
29 #include <libavutil/lfg.h>
30
31 #define ATTR_BOLD 0x01 /** Bold/Bright-foreground (mode 1) */
32 #define ATTR_FAINT 0x02 /** Faint (mode 2) */
33 #define ATTR_UNDERLINE 0x08 /** Underline (mode 4) */
34 #define ATTR_BLINK 0x10 /** Blink/Bright-background (mode 5) */
35 #define ATTR_REVERSE 0x40 /** Reverse (mode 7) */
36 #define ATTR_CONCEALED 0x80 /** Concealed (mode 8) */
37
38 #define DEFAULT_FG_COLOR 7 /** CGA color index */
39 #define DEFAULT_BG_COLOR 0
40 #define DEFAULT_SCREEN_MODE 3 /** 80x25 */
41
42 #define FONT_WIDTH 8 /** Font width */
43
44 /** map ansi color index to cga palette index */
45 static const uint8_t ansi_to_cga[16] = {
46 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15
47 };
48
49 typedef struct {
50 AVFrame frame;
51 int x, y; /** cursor position (pixels) */
52 int sx, sy; /** saved cursor position (pixels) */
53 const uint8_t* font; /** font */
54 int font_height; /** font height */
55 int attributes; /** attribute flags */
56 int fg, bg; /** foreground and background color */
57
58 /* ansi parser state machine */
59 enum {
60 STATE_NORMAL = 0,
61 STATE_ESCAPE,
62 STATE_CODE,
63 STATE_MUSIC_PREAMBLE
64 } state;
65 #define MAX_NB_ARGS 4
66 int args[MAX_NB_ARGS];
67 int nb_args; /** number of arguments (may exceed MAX_NB_ARGS) */
68 } AnsiContext;
69
70 static av_cold int decode_init(AVCodecContext *avctx)
71 {
72 AnsiContext *s = avctx->priv_data;
73 avctx->pix_fmt = PIX_FMT_PAL8;
74
75 /* defaults */
76 s->font = ff_vga16_font;
77 s->font_height = 16;
78 s->fg = DEFAULT_FG_COLOR;
79 s->bg = DEFAULT_BG_COLOR;
80
81 if (!avctx->width || !avctx->height)
82 avcodec_set_dimensions(avctx, 80<<3, 25<<4);
83
84 return 0;
85 }
86
87 static void hscroll(AVCodecContext *avctx)
88 {
89 AnsiContext *s = avctx->priv_data;
90 int i;
91
92 if (s->y < avctx->height - s->font_height) {
93 s->y += s->font_height;
94 return;
95 }
96
97 i = 0;
98 for (; i < avctx->height - s->font_height; i++)
99 memcpy(s->frame.data[0] + i * s->frame.linesize[0],
100 s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
101 avctx->width);
102 for (; i < avctx->height; i++)
103 memset(s->frame.data[0] + i * s->frame.linesize[0],
104 DEFAULT_BG_COLOR, avctx->width);
105 }
106
107 static void erase_line(AVCodecContext * avctx, int xoffset, int xlength)
108 {
109 AnsiContext *s = avctx->priv_data;
110 int i;
111 for (i = 0; i < s->font_height; i++)
112 memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
113 DEFAULT_BG_COLOR, xlength);
114 }
115
116 static void erase_screen(AVCodecContext *avctx)
117 {
118 AnsiContext *s = avctx->priv_data;
119 int i;
120 for (i = 0; i < avctx->height; i++)
121 memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
122 s->x = s->y = 0;
123 }
124
125 /**
126 * Draw character to screen
127 */
128 static void draw_char(AVCodecContext *avctx, int c)
129 {
130 AnsiContext *s = avctx->priv_data;
131 int fg = s->fg;
132 int bg = s->bg;
133
134 if ((s->attributes & ATTR_BOLD))
135 fg += 8;
136 if ((s->attributes & ATTR_BLINK))
137 bg += 8;
138 if ((s->attributes & ATTR_REVERSE))
139 FFSWAP(int, fg, bg);
140 if ((s->attributes & ATTR_CONCEALED))
141 fg = bg;
142 ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
143 s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
144 s->x += FONT_WIDTH;
145 if (s->x >= avctx->width) {
146 s->x = 0;
147 hscroll(avctx);
148 }
149 }
150
151 /**
152 * Execute ANSI escape code
153 * @param <0 error
154 */
155 static int execute_code(AVCodecContext * avctx, int c)
156 {
157 AnsiContext *s = avctx->priv_data;
158 int ret, i, width, height;
159 switch(c) {
160 case 'A': //Cursor Up
161 s->y = FFMAX(s->y - (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), 0);
162 break;
163 case 'B': //Cursor Down
164 s->y = FFMIN(s->y + (s->nb_args > 0 ? s->args[0]*s->font_height : s->font_height), avctx->height - s->font_height);
165 break;
166 case 'C': //Cursor Right
167 s->x = FFMIN(s->x + (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), avctx->width - FONT_WIDTH);
168 break;
169 case 'D': //Cursor Left
170 s->x = FFMAX(s->x - (s->nb_args > 0 ? s->args[0]*FONT_WIDTH : FONT_WIDTH), 0);
171 break;
172 case 'H': //Cursor Position
173 case 'f': //Horizontal and Vertical Position
174 s->y = s->nb_args > 0 ? av_clip((s->args[0] - 1)*s->font_height, 0, avctx->height - s->font_height) : 0;
175 s->x = s->nb_args > 1 ? av_clip((s->args[1] - 1)*FONT_WIDTH, 0, avctx->width - FONT_WIDTH) : 0;
176 break;
177 case 'h': //set creen mode
178 case 'l': //reset screen mode
179 if (s->nb_args < 2)
180 s->args[0] = DEFAULT_SCREEN_MODE;
181 switch(s->args[0]) {
182 case 0: case 1: case 4: case 5: case 13: case 19: //320x200 (25 rows)
183 s->font = ff_cga_font;
184 s->font_height = 8;
185 width = 40<<3;
186 height = 25<<3;
187 break;
188 case 2: case 3: //640x400 (25 rows)
189 s->font = ff_vga16_font;
190 s->font_height = 16;
191 width = 80<<3;
192 height = 25<<4;
193 break;
194 case 6: case 14: //640x200 (25 rows)
195 s->font = ff_cga_font;
196 s->font_height = 8;
197 width = 80<<3;
198 height = 25<<3;
199 break;
200 case 7: //set line wrapping
201 break;
202 case 15: case 16: //640x350 (43 rows)
203 s->font = ff_cga_font;
204 s->font_height = 8;
205 width = 80<<3;
206 height = 43<<3;
207 break;
208 case 17: case 18: //640x480 (60 rows)
209 s->font = ff_cga_font;
210 s->font_height = 8;
211 width = 80<<3;
212 height = 60<<4;
213 break;
214 default:
215 av_log_ask_for_sample(avctx, "unsupported screen mode\n");
216 }
217 if (width != avctx->width || height != avctx->height) {
218 if (s->frame.data[0])
219 avctx->release_buffer(avctx, &s->frame);
220 avcodec_set_dimensions(avctx, width, height);
221 ret = avctx->get_buffer(avctx, &s->frame);
222 if (ret < 0) {
223 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
224 return ret;
225 }
226 s->frame.pict_type = FF_I_TYPE;
227 s->frame.palette_has_changed = 1;
228 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
229 erase_screen(avctx);
230 } else if (c == 'l') {
231 erase_screen(avctx);
232 }
233 break;
234 case 'J': //Erase in Page
235 switch (s->args[0]) {
236 case 0:
237 erase_line(avctx, s->x, avctx->width - s->x);
238 if (s->y < avctx->height - s->font_height)
239 memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
240 DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
241 break;
242 case 1:
243 erase_line(avctx, 0, s->x);
244 if (s->y > 0)
245 memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
246 break;
247 case 2:
248 erase_screen(avctx);
249 }
250 break;
251 case 'K': //Erase in Line
252 switch(s->args[0]) {
253 case 0:
254 erase_line(avctx, s->x, avctx->width - s->x);
255 break;
256 case 1:
257 erase_line(avctx, 0, s->x);
258 break;
259 case 2:
260 erase_line(avctx, 0, avctx->width);
261 }
262 break;
263 case 'm': //Select Graphics Rendition
264 if (s->nb_args == 0) {
265 s->nb_args = 1;
266 s->args[0] = 0;
267 }
268 for (i = 0; i < FFMIN(s->nb_args, MAX_NB_ARGS); i++) {
269 int m = s->args[i];
270 if (m == 0) {
271 s->attributes = 0;
272 s->fg = DEFAULT_FG_COLOR;
273 s->bg = DEFAULT_BG_COLOR;
274 } else if (m == 1 || m == 2 || m == 4 || m == 5 || m == 7 || m == 8) {
275 s->attributes |= 1 << (m - 1);
276 } else if (m >= 30 && m <= 38) {
277 s->fg = ansi_to_cga[m - 30];
278 } else if (m == 39) {
279 s->fg = ansi_to_cga[DEFAULT_FG_COLOR];
280 } else if (m >= 40 && m <= 47) {
281 s->bg = ansi_to_cga[m - 40];
282 } else if (m == 49) {
283 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
284 } else {
285 av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
286 }
287 }
288 break;
289 case 'n': //Device Status Report
290 case 'R': //report current line and column
291 /* ignore */
292 break;
293 case 's': //Save Cursor Position
294 s->sx = s->x;
295 s->sy = s->y;
296 break;
297 case 'u': //Restore Cursor Position
298 s->x = av_clip(s->sx, 0, avctx->width - FONT_WIDTH);
299 s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
300 break;
301 default:
302 av_log_ask_for_sample(avctx, "unsupported escape code\n");
303 break;
304 }
305 return 0;
306 }
307
308 static int decode_frame(AVCodecContext *avctx,
309 void *data, int *data_size,
310 AVPacket *avpkt)
311 {
312 AnsiContext *s = avctx->priv_data;
313 uint8_t *buf = avpkt->data;
314 int buf_size = avpkt->size;
315 const uint8_t *buf_end = buf+buf_size;
316 int ret, i, count;
317
318 ret = avctx->reget_buffer(avctx, &s->frame);
319 if (ret < 0){
320 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
321 return ret;
322 }
323 s->frame.pict_type = FF_I_TYPE;
324 s->frame.palette_has_changed = 1;
325 memcpy(s->frame.data[1], ff_cga_palette, 16 * 4);
326
327 while(buf < buf_end) {
328 switch(s->state) {
329 case STATE_NORMAL:
330 switch (buf[0]) {
331 case 0x00: //NUL
332 case 0x07: //BEL
333 case 0x1A: //SUB
334 /* ignore */
335 break;
336 case 0x08: //BS
337 s->x = FFMAX(s->x - 1, 0);
338 break;
339 case 0x09: //HT
340 i = s->x / FONT_WIDTH;
341 count = ((i + 8) & ~7) - i;
342 for (i = 0; i < count; i++)
343 draw_char(avctx, ' ');
344 break;
345 case 0x0A: //LF
346 hscroll(avctx);
347 case 0x0D: //CR
348 s->x = 0;
349 break;
350 case 0x0C: //FF
351 erase_screen(avctx);
352 break;
353 case 0x1B: //ESC
354 s->state = STATE_ESCAPE;
355 break;
356 default:
357 draw_char(avctx, buf[0]);
358 }
359 break;
360 case STATE_ESCAPE:
361 if (buf[0] == '[') {
362 s->state = STATE_CODE;
363 s->nb_args = 0;
364 s->args[0] = 0;
365 } else {
366 s->state = STATE_NORMAL;
367 draw_char(avctx, 0x1B);
368 return -1;
369 continue;
370 }
371 break;
372 case STATE_CODE:
373 switch(buf[0]) {
374 case '0': case '1': case '2': case '3': case '4':
375 case '5': case '6': case '7': case '8': case '9':
376 if (s->nb_args < MAX_NB_ARGS)
377 s->args[s->nb_args] = s->args[s->nb_args] * 10 + buf[0] - '0';
378 break;
379 case ';':
380 s->nb_args++;
381 if (s->nb_args < MAX_NB_ARGS)
382 s->args[s->nb_args] = 0;
383 break;
384 case 'M':
385 s->state = STATE_MUSIC_PREAMBLE;
386 break;
387 case '=': case '?':
388 /* ignore */
389 break;
390 default:
391 if (s->nb_args > MAX_NB_ARGS)
392 av_log(avctx, AV_LOG_WARNING, "args overflow (%i)\n", s->nb_args);
393 if (s->nb_args < MAX_NB_ARGS && s->args[s->nb_args])
394 s->nb_args++;
395 if (execute_code(avctx, buf[0]) < 0)
396 return -1;
397 s->state = STATE_NORMAL;
398 }
399 break;
400 case STATE_MUSIC_PREAMBLE:
401 if (buf[0] == 0x0E || buf[0] == 0x1B)
402 s->state = STATE_NORMAL;
403 /* ignore music data */
404 break;
405 }
406 buf++;
407 }
408
409 *data_size = sizeof(AVFrame);
410 *(AVFrame*)data = s->frame;
411 return buf_size;
412 }
413
414 static av_cold int decode_close(AVCodecContext *avctx)
415 {
416 AnsiContext *s = avctx->priv_data;
417 if (s->frame.data[0])
418 avctx->release_buffer(avctx, &s->frame);
419 return 0;
420 }
421
422 AVCodec ansi_decoder = {
423 .name = "ansi",
424 .type = CODEC_TYPE_VIDEO,
425 .id = CODEC_ID_ANSI,
426 .priv_data_size = sizeof(AnsiContext),
427 .init = decode_init,
428 .close = decode_close,
429 .decode = decode_frame,
430 .capabilities = CODEC_CAP_DR1,
431 .long_name = NULL_IF_CONFIG_SMALL("ASCII/ANSI art"),
432 };