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