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