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